/* ===== Reset & Base ===== */
*,*::before,*::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
    --green: #007a5e;
    --green-dark: #005a44;
    --green-light: #e6f4f0;
    --gold: #c9a227;
    --blue: #2563eb;
    --red: #dc2626;
    --amber: #d97706;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-500: #64748b;
    --gray-700: #334155;
    --gray-900: #0f172a;
    --radius: 10px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
    --shadow-md: 0 4px 12px rgba(0,0,0,.07);
    --shadow-lg: 0 12px 30px rgba(0,0,0,.10);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

html { font-size: 16px; }
body {
    font-family: var(--font);
    background: var(--gray-50);
    color: var(--gray-900);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.container { max-width: 1280px; margin: 0 auto; padding: 0 20px; width: 100%; }
.main { flex: 1; padding: 30px 20px 60px; }
.main.narrow { max-width: 960px; }
a { color: var(--green); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== Topbar ===== */
.topbar {
    background: linear-gradient(135deg, var(--green-dark), var(--green));
    color: #fff;
    box-shadow: var(--shadow-md);
}
.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    gap: 16px;
    flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-logo { height: 48px; width: auto; background: #fff; padding: 4px; border-radius: 8px; }
.brand-rx { height: 44px; width: auto; background: #fff; padding: 4px 8px; border-radius: 8px; }
.brand-text h1 { font-size: 1.25rem; font-weight: 700; letter-spacing: -.02em; }
.brand-text p { font-size: .8rem; opacity: .85; }

/* ===== Nav ===== */
.mainnav {
    background: #fff;
    border-bottom: 1px solid var(--gray-200);
    position: sticky; top: 0; z-index: 50;
}
.mainnav .container { display: flex; gap: 6px; overflow-x: auto; padding: 0 20px; }
.mainnav a {
    padding: 14px 16px;
    color: var(--gray-700);
    font-weight: 500;
    font-size: .92rem;
    border-bottom: 3px solid transparent;
    white-space: nowrap;
    transition: .15s;
}
.mainnav a:hover { color: var(--green); text-decoration: none; background: var(--gray-50); }
.mainnav a.active { color: var(--green); border-bottom-color: var(--green); font-weight: 600; }

/* ===== Stale Districts Alert ===== */
.stale-panel {
    background: #fffbeb;
    border: 1px solid #fcd34d;
    border-left: 5px solid #f59e0b;
    border-radius: var(--radius);
    padding: 22px 24px;
    margin-bottom: 24px;
    box-shadow: var(--shadow-sm);
}
.stale-head {
    display: flex;
    gap: 14px;
    align-items: flex-start;
    margin-bottom: 16px;
}
.stale-icon { font-size: 2rem; line-height: 1; }
.stale-head h2 {
    font-size: 1.05rem;
    color: #92400e;
    margin-bottom: 4px;
}
.stale-head p {
    font-size: .85rem;
    color: #78350f;
}
.stale-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 12px;
}
.stale-card {
    display: block;
    background: #fff;
    border: 1px solid #fcd34d;
    border-radius: 8px;
    padding: 14px 16px;
    color: inherit;
    transition: .15s;
}
.stale-card:hover {
    box-shadow: var(--shadow-md);
    border-color: #f59e0b;
    text-decoration: none;
    transform: translateY(-2px);
}
.stale-card-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.stale-card-head strong { color: #92400e; font-size: 1rem; }
.stale-badge {
    background: #fef3c7;
    color: #92400e;
    font-size: .72rem;
    font-weight: 700;
    padding: 3px 9px;
    border-radius: 20px;
    text-transform: uppercase;
}
.stale-card-body {
    font-size: .82rem;
    color: #78350f;
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.stale-card-body .sub { color: #a16207; font-weight: 600; }

/* Fresh panel */
.fresh-panel {
    background: #ecfdf5;
    border: 1px solid #a7f3d0;
    border-left: 5px solid #10b981;
    border-radius: var(--radius);
    padding: 20px 24px;
    margin-bottom: 24px;
    display: flex;
    gap: 14px;
    align-items: center;
}
.fresh-icon { font-size: 2rem; }
.fresh-panel h2 { font-size: 1rem; color: #065f46; margin-bottom: 2px; }
.fresh-panel p { font-size: .85rem; color: #047857; }

/* District card stale variant */
.district-card-stale {
    border-color: #fcd34d !important;
    background: #fffdf6 !important;
}
.district-card-foot {
    margin-top: 12px;
    padding-top: 10px;
    border-top: 1px solid var(--gray-100);
    font-size: .78rem;
}
.district-card-foot .sub { color: var(--gray-500); }
.pill-stale {
    background: #fef3c7;
    color: #92400e;
}

/* ===== Activity Strip ===== */
.activity-strip {
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: 18px 22px;
    margin-bottom: 24px;
    box-shadow: var(--shadow-sm);
}
.activity-strip h3 { font-size: .95rem; margin-bottom: 12px; color: var(--gray-900); }
.activity-list { display: flex; flex-direction: column; gap: 8px; }
.activity-item {
    padding: 8px 12px;
    border-radius: 6px;
    background: var(--gray-50);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    font-size: .85rem;
}
.activity-main strong { color: var(--gray-900); }
.activity-main .sub { color: var(--gray-500); font-size: .82rem; }
.activity-meta {
    display: flex;
    gap: 6px;
    align-items: center;
    color: var(--gray-500);
    font-size: .8rem;
    flex-wrap: wrap;
}
.activity-meta .dot { color: var(--gray-300); }
.activity-more {
    display: inline-block;
    margin-top: 10px;
    font-size: .85rem;
    font-weight: 600;
}

/* IP code style */
.ip-code {
    background: var(--gray-100);
    color: var(--green-dark);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: .72rem;
    font-family: "SFMono-Regular", Consolas, monospace;
}

/* ===== Hero ===== */
.hero {
    display: flex;
    align-items: center;
    gap: 30px;
    background: #fff;
    border-radius: var(--radius);
    padding: 30px;
    box-shadow: var(--shadow-sm);
    margin-bottom: 28px;
    border: 1px solid var(--gray-200);
}
.hero-content { flex: 1; }
.hero h2 { font-size: 1.5rem; margin-bottom: 8px; color: var(--gray-900); }
.hero p { color: var(--gray-500); max-width: 600px; }
.hero-img { max-height: 120px; border-radius: 8px; object-fit: cover; }

/* ===== District cards grid ===== */
.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 18px;
}
.district-card {
    display: block;
    background: #fff;
    border-radius: var(--radius);
    padding: 20px;
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
    transition: .2s;
    color: inherit;
}
.district-card:hover {
    box-shadow: var(--shadow-lg);
    transform: translateY(-3px);
    border-color: var(--green);
    text-decoration: none;
}
.district-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}
.district-card h3 { font-size: 1.1rem; color: var(--green-dark); }
.pill {
    font-size: .75rem;
    font-weight: 700;
    padding: 3px 10px;
    border-radius: 20px;
}
.pill-good { background: #d1fae5; color: #065f46; }
.pill-mid  { background: #fef3c7; color: #92400e; }
.pill-low  { background: #fee2e2; color: #991b1b; }
.district-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin-bottom: 14px;
    text-align: center;
}
.district-stats strong { display: block; font-size: 1.25rem; color: var(--gray-900); }
.district-stats span { font-size: .7rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: .04em; }
.progress-bar {
    height: 6px;
    background: var(--gray-100);
    border-radius: 6px;
    overflow: hidden;
}
.progress-bar span {
    display: block;
    height: 100%;
    background: linear-gradient(90deg, var(--green), #00a884);
    border-radius: 6px;
    transition: width .4s;
}

/* ===== District header ===== */
.district-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    background: #fff;
    padding: 22px 26px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    margin-bottom: 22px;
    border: 1px solid var(--gray-200);
}
.district-header h2 { margin-top: 4px; font-size: 1.5rem; }
.back-link { font-size: .85rem; color: var(--gray-500); }
.district-header-img { max-height: 80px; border-radius: 8px; }

/* ===== Stat bar ===== */
.stat-bar {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 12px;
    margin-bottom: 26px;
}
.stat-box {
    background: #fff;
    border-radius: var(--radius);
    padding: 16px 18px;
    border: 1px solid var(--gray-200);
    border-left: 4px solid var(--gray-300);
    box-shadow: var(--shadow-sm);
}
.stat-box.stat-good { border-left-color: var(--green); }
.stat-box.stat-info { border-left-color: var(--blue); }
.stat-box.stat-warn { border-left-color: var(--amber); }
.stat-num { display: block; font-size: 1.6rem; font-weight: 700; color: var(--gray-900); }
.stat-label { font-size: .72rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: .05em; }

/* ===== Municipality sections ===== */
.muni-section { margin-bottom: 32px; }
.muni-title {
    font-size: 1.05rem;
    color: var(--gray-900);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.muni-count {
    font-size: .72rem;
    background: var(--green-light);
    color: var(--green-dark);
    padding: 3px 10px;
    border-radius: 20px;
    font-weight: 600;
}

/* ===== Tables ===== */
.table-wrap {
    background: #fff;
    border-radius: var(--radius);
    border: 1px solid var(--gray-200);
    overflow-x: auto;
    box-shadow: var(--shadow-sm);
}
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: .88rem;
    min-width: 900px;
}
.data-table thead {
    background: var(--gray-50);
    border-bottom: 2px solid var(--gray-200);
}
.data-table th {
    text-align: left;
    padding: 12px 14px;
    font-size: .72rem;
    font-weight: 700;
    color: var(--gray-500);
    text-transform: uppercase;
    letter-spacing: .05em;
    white-space: nowrap;
}
.data-table td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--gray-100);
    vertical-align: middle;
}
.data-table tbody tr:hover { background: var(--gray-50); }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table .sub { font-size: .78rem; color: var(--gray-500); }

/* ===== Badges ===== */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: .72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .03em;
    white-space: nowrap;
}
.badge-yes  { background: #d1fae5; color: #065f46; }
.badge-no   { background: #fee2e2; color: #991b1b; }
.badge-warn { background: #fef3c7; color: #92400e; }
.badge-na   { background: var(--gray-100); color: var(--gray-500); }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: .9rem;
    font-weight: 600;
    border: 1px solid transparent;
    cursor: pointer;
    transition: .15s;
    text-decoration: none;
}
.btn-sm { padding: 6px 12px; font-size: .82rem; }
.btn-primary {
    background: var(--green);
    color: #fff;
    border-color: var(--green);
}
.btn-primary:hover { background: var(--green-dark); border-color: var(--green-dark); text-decoration: none; }
.btn-ghost {
    background: #fff;
    color: var(--gray-700);
    border-color: var(--gray-300);
}
.btn-ghost:hover { background: var(--gray-50); text-decoration: none; }
.btn-sm.btn-primary, .btn.btn-sm { background: var(--green-light); color: var(--green-dark); border-color: var(--green-light); }
.btn-sm:hover { background: var(--green); color: #fff; text-decoration: none; }

/* ===== Alerts ===== */
.alert {
    padding: 14px 18px;
    border-radius: var(--radius);
    margin-bottom: 18px;
    font-size: .92rem;
    border-left: 4px solid;
}
.alert-success { background: #ecfdf5; color: #065f46; border-left-color: #10b981; }
.alert-error   { background: #fef2f2; color: #991b1b; border-left-color: #ef4444; }
.alert-warn    { background: #fffbeb; color: #92400e; border-left-color: #f59e0b; }
.error { color: var(--red); padding: 20px; }

/* ===== IP Notice on update form ===== */
.ip-notice {
    background: var(--gray-100);
    border-left: 4px solid var(--green);
    padding: 10px 14px;
    border-radius: 8px;
    font-size: .85rem;
    color: var(--gray-700);
    margin-bottom: 18px;
}
.ip-notice code {
    background: #fff;
    padding: 1px 6px;
    border-radius: 4px;
    color: var(--green-dark);
    font-weight: 600;
    font-family: "SFMono-Regular", Consolas, monospace;
    margin-left: 4px;
}

/* ===== Record header ===== */
.record-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    background: #fff;
    border-radius: var(--radius);
    padding: 22px 26px;
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
    margin-bottom: 22px;
}
.record-header h2 { font-size: 1.4rem; margin: 4px 0; }
.record-header .crumb { font-size: .82rem; color: var(--green); font-weight: 600; }
.record-header .sub { font-size: .82rem; color: var(--gray-500); }
.record-header-img { max-height: 70px; border-radius: 8px; }

/* ===== Forms ===== */
.update-form { display: flex; flex-direction: column; gap: 20px; }
.form-section {
    background: #fff;
    border-radius: var(--radius);
    padding: 22px 24px;
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
}
.form-section h3 { font-size: 1rem; margin-bottom: 16px; color: var(--gray-900); }
.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 16px;
}
.form-group { display: flex; flex-direction: column; }
.form-group.full { grid-column: 1 / -1; }
.form-group label {
    font-size: .8rem;
    font-weight: 600;
    color: var(--gray-700);
    margin-bottom: 6px;
}
.form-group .req { color: var(--red); }
.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--gray-300);
    border-radius: 8px;
    font-size: .92rem;
    font-family: inherit;
    background: #fff;
    transition: .15s;
    color: var(--gray-900);
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--green);
    box-shadow: 0 0 0 3px rgba(0,122,94,.12);
}
.form-group small { font-size: .75rem; color: var(--gray-500); margin-top: 4px; }

.form-actions {
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    padding: 8px 0;
}

/* ===== History (update page) ===== */
.history-section {
    margin-top: 30px;
    background: #fff;
    border-radius: var(--radius);
    padding: 22px 24px;
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
}
.history-section h3 { font-size: 1rem; margin-bottom: 6px; }
.history-list { display: flex; flex-direction: column; gap: 10px; max-height: 500px; overflow-y: auto; margin-top: 12px; }
.history-item {
    border-left: 3px solid var(--green);
    padding: 10px 14px;
    background: var(--gray-50);
    border-radius: 6px;
    font-size: .85rem;
}
.history-meta { display: flex; gap: 12px; align-items: center; margin-bottom: 4px; flex-wrap: wrap; }
.history-meta strong { color: var(--gray-900); }
.history-meta span { color: var(--gray-500); font-size: .78rem; }
.history-body .field { font-family: monospace; background: #fff; padding: 1px 6px; border-radius: 4px; font-size: .8rem; color: var(--green-dark); }
.old { color: var(--red); text-decoration: line-through; }
.new { color: var(--green-dark); font-weight: 600; }
.arrow { color: var(--gray-500); margin: 0 6px; }

/* ===== Empty / Footer ===== */
.empty { text-align: center; padding: 40px; color: var(--gray-500); }
.footer {
    background: #fff;
    border-top: 1px solid var(--gray-200);
    padding: 16px 0;
    text-align: center;
    font-size: .8rem;
    color: var(--gray-500);
    margin-top: auto;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .topbar-inner { padding: 12px 16px; }
    .brand-text h1 { font-size: 1.05rem; }
    .brand-text p { font-size: .72rem; }
    .brand-logo, .brand-rx { height: 40px; }
    .hero { flex-direction: column; align-items: flex-start; padding: 22px; }
    .hero-img { max-height: 80px; }
    .district-header, .record-header { flex-direction: column; align-items: flex-start; }
    .district-header-img, .record-header-img { max-height: 60px; }
    .form-row { grid-template-columns: 1fr; }
    .form-actions { flex-direction: column-reverse; }
    .form-actions .btn { width: 100%; justify-content: center; }
    .main { padding: 20px 16px 40px; }
    .stat-bar { grid-template-columns: repeat(2, 1fr); }
    .data-table { font-size: .82rem; }
    .data-table th, .data-table td { padding: 10px; }
    .stale-head { flex-direction: column; }
    .activity-item { flex-direction: column; align-items: flex-start; }
}

@media (max-width: 480px) {
    .cards-grid { grid-template-columns: 1fr; }
    .district-stats { grid-template-columns: repeat(3, 1fr); }
    .stale-grid { grid-template-columns: 1fr; }
}