:root {
    --ink: #17202a;
    --muted: #607080;
    --line: rgba(23, 32, 42, 0.14);
    --panel: rgba(255, 255, 255, 0.86);
    --shadow: 0 18px 46px rgba(31, 42, 68, 0.14);
    --blue: #147fbd;
    --green: #168c5a;
    --red: #dc3f3f;
    --gold: #b98218;
    --teal: #176b87;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    font-family: 'Noto Sans Thai', sans-serif;
    background:
        radial-gradient(circle at 12% 6%, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0) 28rem),
        linear-gradient(135deg, rgba(224, 239, 232, 0.95), rgba(237, 244, 248, 0.9) 48%, rgba(250, 246, 234, 0.78)),
        #eef4f2;
}

.dashboard-page,
.report-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.app-header {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin: clamp(0.75rem, 2vw, 1.25rem) clamp(1rem, 3vw, 2rem) 0;
    padding: 1rem 1.1rem;
    border: 1px solid rgba(23, 32, 42, 0.1);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.72);
    box-shadow: 0 14px 34px rgba(31, 42, 68, 0.08);
    backdrop-filter: blur(12px);
}

.eyebrow {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0 0 0.5rem;
    padding: 0.4rem 0.86rem;
    border: 1px solid rgba(23, 107, 135, 0.22);
    border-radius: 8px;
    color: #11506a;
    background: rgba(232, 246, 243, 0.82);
    box-shadow: 0 8px 18px rgba(23, 107, 135, 0.08);
    font-size: 1.28rem;
    font-weight: 900;
}

.eyebrow::before {
    content: '';
    width: 0.68rem;
    height: 0.68rem;
    border-radius: 50%;
    background: var(--teal);
    box-shadow: 0 0 0 0.22rem rgba(23, 107, 135, 0.12);
}

h1,
h2,
h3 {
    margin: 0;
    line-height: 1.18;
}

h1,
h3 {
    font-size: clamp(1.45rem, 3vw, 2.25rem);
    color: #102636;
    letter-spacing: 0;
}

h2 {
    font-size: 1.05rem;
}

.developer-credit {
    margin: 0.45rem 0 0;
    color: var(--muted);
    font-size: 0.95rem;
    font-weight: 700;
    line-height: 1.35;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

button,
a.primary-button,
a.ghost-button {
    min-height: 2.45rem;
    padding: 0.55rem 0.85rem;
    border: 1px solid transparent;
    border-radius: 8px;
    font: inherit;
    font-weight: 700;
    text-decoration: none;
    cursor: pointer;
}

.primary-button {
    color: #fff;
    background: linear-gradient(135deg, var(--teal), #0f5975);
    border-color: rgba(15, 89, 117, 0.9);
    box-shadow: 0 10px 22px rgba(23, 107, 135, 0.24);
}

.ghost-button {
    color: #143d59;
    background: rgba(255, 255, 255, 0.78);
    border-color: rgba(20, 61, 89, 0.22);
}

.primary-button:hover,
.ghost-button:hover {
    transform: translateY(-1px);
}

button:disabled {
    opacity: 0.65;
    cursor: progress;
}

.app-shell {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
    flex: 1 1 auto;
    min-height: 0;
    padding: 0.75rem clamp(1rem, 3vw, 2rem) 1rem;
}

.dashboard-shell {
    grid-template-rows: minmax(0, 1fr);
}

.report-shell {
    grid-template-rows: minmax(0, 1fr);
    overflow: hidden;
}

.map-panel,
.table-panel {
    min-width: 0;
}

.map-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 0.9rem;
    height: 100%;
    min-height: 0;
}

.stats-grid {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.65rem;
    overflow-x: auto;
    padding-bottom: 0.2rem;
    scrollbar-width: thin;
}

.stats-grid article {
    flex: 1 1 0;
    min-width: 0;
    position: relative;
    padding: 0.9rem 1rem 0.95rem;
    border: 1px solid rgba(23, 32, 42, 0.09);
    border-radius: 8px;
    overflow: hidden;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(255, 255, 255, 0.76));
    box-shadow: 0 12px 26px rgba(31, 42, 68, 0.1);
}

.stats-grid article::before {
    content: '';
    position: absolute;
    inset: 0 auto 0 0;
    width: 0.28rem;
    background: var(--accent, var(--teal));
}

.stat-total { --accent: var(--teal); }
.stat-radio-good { --accent: var(--blue); }
.stat-radio-bad { --accent: var(--red); }
.stat-tower-good { --accent: var(--green); }
.stat-tower-bad { --accent: var(--gold); }

.stats-grid article:hover {
    transform: translateY(-1px);
    box-shadow: 0 16px 32px rgba(31, 42, 68, 0.14);
}

.stats-grid span {
    display: inline-flex;
    align-items: center;
    max-width: 100%;
    padding: 0.18rem 0.46rem;
    border-radius: 8px;
    color: var(--accent, var(--teal));
    background: color-mix(in srgb, var(--accent, var(--teal)) 11%, transparent);
    font-size: 1rem;
    font-weight: 900;
    line-height: 1.25;
    white-space: nowrap;
}

.stats-grid strong {
    display: block;
    margin-top: 0.24rem;
    color: #102636;
    font-size: 1.65rem;
    line-height: 1;
}

.map-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 16rem;
    gap: 0.85rem;
    min-height: 0;
}

.map-canvas {
    width: 100%;
    height: 100%;
    min-height: 0;
    border: 1px solid rgba(23, 32, 42, 0.12);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: var(--shadow), inset 0 0 0 1px rgba(255, 255, 255, 0.4);
    background: #dbe8ea;
}

.map-sidebar {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    align-self: stretch;
    min-height: 0;
    overflow: auto;
}

.layer-control,
.legend {
    padding: 0.75rem;
    border: 1px solid rgba(23, 32, 42, 0.1);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.76);
    box-shadow: 0 12px 26px rgba(31, 42, 68, 0.08);
    color: var(--muted);
    font-size: 0.9rem;
}

.layer-control,
.legend {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
}

.layer-control strong,
.legend strong {
    color: #102636;
    font-size: 0.95rem;
}

.layer-control label {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    padding: 0.45rem 0.55rem;
    border: 1px solid rgba(23, 32, 42, 0.09);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.68);
    color: #34495e;
    cursor: pointer;
}

.layer-control input {
    width: 1rem;
    height: 1rem;
    accent-color: var(--teal);
}

.legend span {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.36rem 0.55rem;
    border: 1px solid rgba(23, 32, 42, 0.09);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.68);
}

.legend-polygon,
.legend-dot,
.legend-line {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    flex: 0 0 auto;
}

.legend-polygon {
    border: 2px solid currentColor;
    background: rgba(255, 255, 255, 0.3);
}

.legend-dot {
    border-radius: 50%;
    border: 2px solid #fff;
}

.legend-line {
    height: 0;
    border-top: 3px dashed currentColor;
}

.is-blue { color: #1594d2; background: #1594d2; }
.is-red { color: #dc3f3f; background: #dc3f3f; }
.is-green { color: #168c5a; background: #168c5a; }
.is-district { color: #182f3d; }
.is-subdistrict { color: #b98218; }
.is-village { color: #52606d; }

.table-panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.report-page .table-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100%;
    min-height: 0;
}

.table-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 1rem;
    border-bottom: 1px solid var(--line);
}

.table-toolbar p {
    margin: 0.25rem 0 0;
    color: var(--muted);
    font-size: 0.88rem;
}

.table-filters {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.55rem;
    flex-wrap: wrap;
    flex: 1 1 38rem;
    min-width: min(100%, 28rem);
}

.table-toolbar input,
.table-toolbar select {
    min-height: 2.4rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--line);
    border-radius: 8px;
    font: inherit;
}

.table-toolbar input {
    width: min(23rem, 100%);
}

.table-toolbar select {
    width: min(13rem, 100%);
    background: #fff;
}

.login-shell {
    place-items: center;
    min-height: calc(100vh - 8rem);
}

.login-panel {
    width: min(28rem, 100%);
    padding: 1.3rem;
    border: 1px solid rgba(23, 32, 42, 0.12);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.86);
    box-shadow: var(--shadow);
}

.login-panel p {
    margin: 0.35rem 0 0;
    color: var(--muted);
}

.login-form {
    display: grid;
    gap: 0.85rem;
    margin-top: 1rem;
}

.login-form label {
    display: grid;
    gap: 0.35rem;
    color: #34495e;
    font-weight: 800;
}

.login-form input {
    min-height: 2.55rem;
    padding: 0.55rem 0.75rem;
    border: 1px solid rgba(23, 32, 42, 0.16);
    border-radius: 8px;
    font: inherit;
}

.login-error {
    padding: 0.55rem 0.7rem;
    border: 1px solid rgba(220, 63, 63, 0.22);
    border-radius: 8px;
    background: rgba(220, 63, 63, 0.1);
    color: #a33333 !important;
    font-weight: 800;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: rgba(16, 38, 54, 0.42);
    backdrop-filter: blur(6px);
}

.modal-backdrop[hidden] {
    display: none;
}

.modal-panel {
    width: min(24rem, 100%);
    padding: 1.15rem;
    border: 1px solid rgba(23, 32, 42, 0.12);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 24px 60px rgba(16, 38, 54, 0.24);
    text-align: center;
}

.modal-panel p {
    margin: 0.55rem 0 1rem;
    color: var(--muted);
}

body.modal-open {
    overflow: hidden;
}

.spreadsheet-wrap {
    overflow: auto;
    max-height: calc(100vh - 12rem);
}

.report-page .spreadsheet-wrap {
    min-height: 0;
    max-height: none;
}

.spreadsheet {
    width: 100%;
    min-width: 76rem;
    border-collapse: collapse;
    background: #fff;
}

.spreadsheet th,
.spreadsheet td {
    padding: 0.45rem 0.55rem;
    border: 1px solid rgba(23, 32, 42, 0.1);
    vertical-align: middle;
    text-align: left;
    font-size: 0.88rem;
}

.spreadsheet th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #edf4f3;
    font-weight: 700;
}

.spreadsheet select,
.spreadsheet input {
    width: 100%;
    min-width: 9rem;
    padding: 0.42rem 0.5rem;
    border: 1px solid rgba(23, 32, 42, 0.14);
    border-radius: 6px;
    font: inherit;
    background: #fff;
}

.status-received,
.status-working { background: rgba(22, 140, 90, 0.12); }
.status-not_received,
.status-broken { background: rgba(220, 63, 63, 0.12); }
.status-unknown { background: rgba(123, 135, 148, 0.12); }

.empty-row,
.map-error {
    padding: 1.25rem;
    color: #8a3f2b;
    text-align: center;
}

@media (max-width: 1180px) and (min-width: 781px) {
    .app-header {
        align-items: flex-start;
    }

    .header-actions {
        flex-wrap: wrap;
        justify-content: flex-end;
    }

    .eyebrow {
        font-size: 1rem;
    }

    h1,
    h3 {
        font-size: 1.55rem;
    }

    .app-shell {
        padding-inline: 1rem;
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        overflow: visible;
    }

    .stats-grid article {
        padding: 0.75rem 0.82rem;
    }

    .stats-grid span {
        white-space: normal;
    }

    .map-layout {
        grid-template-columns: minmax(0, 1fr) 13.5rem;
        gap: 0.7rem;
    }

    .layer-control,
    .legend {
        padding: 0.65rem;
        font-size: 0.82rem;
    }

    .table-toolbar {
        align-items: flex-start;
        flex-wrap: wrap;
    }

    .table-filters {
        justify-content: flex-start;
        flex: 1 1 100%;
        min-width: 0;
    }

    .table-toolbar select {
        flex: 1 1 11rem;
        width: auto;
    }

    .table-toolbar input {
        flex: 2 1 18rem;
        width: auto;
    }

    .spreadsheet-wrap {
        max-height: calc(100vh - 15rem);
    }

    .spreadsheet {
        min-width: 68rem;
    }
}

@media (max-width: 780px) {
    .app-header,
    .table-toolbar {
        align-items: stretch;
        flex-direction: column;
    }

    .header-actions {
        flex-wrap: wrap;
    }

    .table-filters {
        align-items: stretch;
        flex-direction: column;
        min-width: 0;
        width: 100%;
    }

    .table-toolbar input,
    .table-toolbar select {
        width: 100%;
    }

    .stats-grid {
        overflow-x: auto;
    }

    .stats-grid article {
        flex: 0 0 10.5rem;
    }

    .map-panel {
        grid-template-rows: auto minmax(0, 1fr);
    }

    .map-layout {
        grid-template-columns: minmax(0, 1fr) 12rem;
        min-height: 0;
        gap: 0.65rem;
    }

    .map-canvas {
        min-height: 0;
    }

    .layer-control,
    .legend {
        padding: 0.6rem;
        font-size: 0.78rem;
    }
}