:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-2: #eef3f1;
    --text: #17202a;
    --muted: #6b7785;
    --border: #dce3e8;
    --green: #007a53;
    --green-dark: #005f43;
    --blue: #2855d8;
    --yellow: #f0b429;
    --red: #c0392b;
    --slate: #34495e;
    --shadow: 0 12px 28px rgba(23, 32, 42, 0.09);
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    color: var(--text);
    background: var(--bg);
    font-family: Arial, Helvetica, sans-serif;
}

a {
    color: inherit;
    text-decoration: none;
}

button,
input,
select,
textarea {
    font: inherit;
}

.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
}

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    padding: 22px 16px;
    display: flex;
    flex-direction: column;
    background: #13221f;
    color: #f8fbfa;
}

.brand {
    display: grid;
    grid-template-columns: 42px 1fr;
    gap: 12px;
    align-items: center;
    min-width: 0;
}

.brand-mark {
    width: 42px;
    height: 42px;
    display: inline-grid;
    place-items: center;
    border-radius: 8px;
    background: var(--green);
    font-weight: 800;
}

.brand strong,
.brand small {
    display: block;
}

.brand strong {
    line-height: 1.1;
}

.brand small {
    margin-top: 3px;
    color: #bdd1cb;
    font-size: 12px;
}

.nav-list {
    margin-top: 34px;
    display: grid;
    gap: 6px;
}

.nav-list a {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    padding: 0 12px;
    border-radius: 8px;
    color: #d7e4df;
}

.nav-list a:hover,
.nav-list a.active {
    background: rgba(255, 255, 255, 0.1);
    color: #ffffff;
}

.nav-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #78958c;
}

.nav-list a.active .nav-dot {
    background: var(--yellow);
}

.sidebar-footer {
    margin-top: auto;
    padding-top: 18px;
    border-top: 1px solid rgba(255, 255, 255, 0.12);
    display: grid;
    gap: 8px;
    color: #d7e4df;
    font-size: 14px;
}

.sidebar-footer a {
    color: #ffffff;
    font-weight: 700;
}

.main {
    min-width: 0;
    padding: 24px;
}

.topbar {
    min-height: 70px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
}

.topbar > div {
    min-width: 0;
}

.topbar h1 {
    margin: 4px 0 0;
    font-size: 28px;
    line-height: 1.1;
    letter-spacing: 0;
}

.eyebrow {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0;
}

.sidebar-toggle {
    display: none;
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
}

.sidebar-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    margin: 0;
    background: var(--text);
}

.notice {
    margin: 16px 0;
    padding: 12px 14px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
}

.notice.success {
    border-color: rgba(0, 122, 83, 0.3);
    background: #edf8f4;
}

.notice.error {
    border-color: rgba(192, 57, 43, 0.3);
    background: #fff0ee;
}

.login-page {
    min-height: 100vh;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(360px, 460px);
}

.login-visual {
    padding: 48px;
    display: flex;
    align-items: flex-end;
    color: #ffffff;
    background:
        linear-gradient(135deg, rgba(19, 34, 31, 0.92), rgba(0, 122, 83, 0.7)),
        url("/assets/img/login-visual.jpg") center/cover;
}

.login-visual h1 {
    max-width: 680px;
    margin: 0;
    font-size: 44px;
    line-height: 1.05;
    letter-spacing: 0;
}

.login-panel {
    padding: 42px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    background: var(--surface);
}

.login-panel h2 {
    margin: 0 0 8px;
    font-size: 28px;
}

.login-panel p {
    margin: 0 0 26px;
    color: var(--muted);
}

.form-grid {
    display: grid;
    gap: 14px;
}

.form-row {
    display: grid;
    gap: 7px;
}

.form-row label {
    color: var(--slate);
    font-size: 14px;
    font-weight: 700;
}

.form-row input,
.form-row select,
.form-row textarea {
    width: 100%;
    min-height: 42px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #ffffff;
    color: var(--text);
}

.password-field {
    position: relative;
}

.password-field input {
    padding-right: 48px;
}

.password-toggle {
    position: absolute;
    top: 50%;
    right: 5px;
    transform: translateY(-50%);
    width: 34px;
    height: 34px;
    display: inline-grid;
    place-items: center;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.password-toggle:hover,
.password-toggle:focus-visible {
    background: var(--surface-2);
    color: var(--text);
}

.password-toggle:focus-visible {
    outline: 2px solid rgba(40, 85, 216, 0.35);
    outline-offset: 2px;
}

.password-toggle::before {
    content: "";
    width: 18px;
    height: 11px;
    border: 2px solid currentColor;
    border-radius: 50% / 60%;
}

.password-toggle::after {
    content: "";
    position: absolute;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

.password-toggle.is-visible::after {
    width: 20px;
    height: 2px;
    border-radius: 999px;
    transform: rotate(-35deg);
}

.form-row textarea {
    min-height: 90px;
    resize: vertical;
}

.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
}

.btn {
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 14px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: var(--green);
    color: #ffffff;
    font-weight: 700;
    cursor: pointer;
}

.btn:hover {
    background: var(--green-dark);
}

.btn.secondary {
    background: #ffffff;
    color: var(--text);
    border-color: var(--border);
}

.btn.secondary:hover {
    background: var(--surface-2);
}

.btn.danger {
    background: #b42318;
    color: #ffffff;
}

.btn.danger:hover {
    background: #8f1d14;
}

.btn.compact {
    min-height: 36px;
    padding: 0 12px;
}

.dashboard-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    gap: 18px;
    align-items: stretch;
}

.map-frame {
    min-height: calc(100vh - 126px);
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
    background: var(--surface);
    box-shadow: var(--shadow);
}

#toiletMap {
    width: 100%;
    height: 100%;
    min-height: 560px;
}

.toilet-marker-icon {
    background: transparent;
    border: 0;
}

.toilet-marker {
    position: relative;
    width: 38px;
    height: 46px;
    display: block;
    filter: drop-shadow(0 8px 12px rgba(23, 32, 42, 0.3));
}

.toilet-marker::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 2px;
    width: 32px;
    height: 32px;
    transform: translateX(-50%);
    border: 3px solid #ffffff;
    border-radius: 50%;
    background: var(--marker-color, var(--blue));
    box-shadow: 0 0 0 3px rgba(23, 32, 42, 0.16);
    z-index: 2;
}

.toilet-marker::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 27px;
    width: 16px;
    height: 16px;
    transform: translateX(-50%) rotate(45deg);
    border-right: 3px solid #ffffff;
    border-bottom: 3px solid #ffffff;
    border-radius: 0 0 5px 0;
    background: var(--marker-color, var(--blue));
    z-index: 1;
}

.toilet-marker-core {
    position: absolute;
    left: 50%;
    top: 15px;
    z-index: 3;
    width: 11px;
    height: 11px;
    display: block;
    transform: translateX(-50%);
    border: 2px solid rgba(23, 32, 42, 0.18);
    border-radius: 50%;
    background: #ffffff;
}

.side-panel {
    display: grid;
    align-content: start;
    gap: 14px;
}

.panel,
.device-card,
.table-panel {
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.panel {
    padding: 16px;
}

.access-denied {
    max-width: 620px;
    padding: 24px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.access-denied-mark {
    width: 42px;
    height: 42px;
    display: inline-grid;
    place-items: center;
    margin-bottom: 14px;
    border-radius: 8px;
    background: #fff0ee;
    color: var(--red);
    font-size: 24px;
    font-weight: 800;
}

.access-denied h2 {
    margin: 4px 0 10px;
    font-size: 24px;
}

.access-denied p:not(.eyebrow) {
    max-width: 520px;
    margin: 0 0 18px;
    color: var(--slate);
    line-height: 1.55;
}

.panel h2,
.table-panel h2 {
    margin: 0 0 14px;
    font-size: 18px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.stat {
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fbfcfd;
}

.stat span {
    display: block;
    color: var(--muted);
    font-size: 13px;
}

.stat strong {
    display: block;
    margin-top: 4px;
    font-size: 26px;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    min-height: 28px;
    padding: 0 10px;
    border-radius: 999px;
    color: #ffffff;
    font-size: 13px;
    font-weight: 700;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.4);
}

.status-online {
    background: var(--green);
}

.status-warning {
    background: var(--yellow);
    color: #2d2400;
}

.status-alert {
    background: var(--red);
}

.status-offline {
    background: #7b8794;
}

.live-status {
    margin: -6px 0 12px;
    color: var(--green-dark);
    font-size: 12px;
    font-weight: 700;
}

.live-status.inline {
    margin: 0 0 0 8px;
}

.live-status.is-error {
    color: var(--red);
}

.device-list {
    display: grid;
    gap: 10px;
}

.map-device-row {
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
}

.map-device-row:hover {
    border-color: var(--green);
}

.map-device-row strong,
.map-device-row small {
    display: block;
}

.map-device-row small {
    margin-top: 4px;
    color: var(--muted);
}

.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 16px;
}

.device-card {
    padding: 16px;
    display: grid;
    gap: 14px;
}

.device-card header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.device-card h2 {
    margin: 0;
    font-size: 18px;
    line-height: 1.2;
}

.device-card p {
    margin: 4px 0 0;
    color: var(--muted);
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.metric {
    padding: 10px;
    border-radius: 8px;
    background: var(--surface-2);
}

.metric span,
.detail-list dt {
    color: var(--muted);
    font-size: 12px;
}

.metric strong {
    display: block;
    margin-top: 3px;
    font-size: 17px;
}

.detail-list {
    margin: 0;
    display: grid;
    grid-template-columns: 95px minmax(0, 1fr);
    gap: 7px 10px;
}

.detail-list dd {
    margin: 0;
    min-width: 0;
    word-break: break-word;
}

.payload-preview {
    max-height: 170px;
    overflow: auto;
    margin: 0;
    padding: 10px;
    border-radius: 8px;
    background: #13221f;
    color: #d8fff1;
    font-size: 12px;
}

.toolbar {
    margin: 12px 0 18px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: space-between;
    align-items: center;
}

.toolbar p {
    margin: 0;
    color: var(--muted);
}

.form-panel {
    margin-bottom: 18px;
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
}

.form-panel h2 {
    margin: 0 0 14px;
    font-size: 18px;
}

.form-compact {
    display: grid;
    grid-template-columns: repeat(4, minmax(150px, 1fr));
    gap: 12px;
    align-items: end;
}

.form-compact .wide {
    grid-column: span 2;
}

.modal {
    width: min(980px, calc(100vw - 28px));
    max-height: calc(100vh - 28px);
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
}

.modal::backdrop {
    background: rgba(19, 34, 31, 0.6);
}

.modal-panel {
    max-height: calc(100vh - 28px);
    overflow: auto;
    padding: 18px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    align-items: flex-start;
    margin-bottom: 16px;
}

.modal-header h2 {
    margin: 4px 0 0;
    font-size: 22px;
}

.modal-form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.modal-form-grid .wide {
    grid-column: 1 / -1;
}

.device-map-toolbar {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
}

.device-picker-map {
    width: 100%;
    min-height: 320px;
    margin-top: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
    background: var(--surface-2);
}

.field-help {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.modal-actions {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    align-items: center;
}

.table-panel {
    padding: 16px;
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th,
.data-table td {
    padding: 11px 10px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}

.data-table th {
    color: var(--muted);
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0;
}

.table-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.logs-filter {
    align-items: center;
}

.logs-filter select {
    min-width: 170px;
}

.logs-table td small {
    display: block;
    max-width: 260px;
    margin-top: 4px;
    color: var(--muted);
    line-height: 1.35;
    word-break: break-word;
}

.log-measures {
    min-width: 105px;
    margin: 0;
    display: grid;
    grid-template-columns: 48px minmax(0, 1fr);
    gap: 4px 8px;
}

.log-measures dt {
    color: var(--muted);
    font-size: 12px;
}

.log-measures dd {
    margin: 0;
}

.payload-details {
    min-width: 250px;
}

.payload-details summary {
    cursor: pointer;
    color: var(--green-dark);
    font-weight: 700;
}

.logs-payload {
    width: min(520px, 72vw);
    max-height: 260px;
    margin-top: 8px;
}

.pagination {
    margin-top: 14px;
    display: flex;
    gap: 10px;
    align-items: center;
}

.pagination span {
    color: var(--muted);
}

.muted {
    color: var(--muted);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.leaflet-popup-content {
    min-width: 220px;
}

.leaflet-popup-content h3 {
    margin: 0 0 8px;
    font-size: 16px;
}

.popup-meta {
    display: grid;
    gap: 4px;
}

.guide-layout {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
    gap: 18px;
    align-items: start;
}

.guide-toc {
    position: sticky;
    top: 24px;
    padding: 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.guide-toc h2 {
    margin: 0 0 12px;
    font-size: 17px;
}

.guide-toc a {
    display: block;
    padding: 9px 0;
    border-top: 1px solid var(--border);
    color: var(--slate);
    font-weight: 700;
}

.guide-toc a:hover {
    color: var(--green);
}

.guide-content {
    display: grid;
    gap: 16px;
}

.guide-section {
    padding: 18px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    box-shadow: var(--shadow);
    scroll-margin-top: 20px;
}

.guide-section h2 {
    margin: 4px 0 10px;
    font-size: 22px;
}

.guide-section h3 {
    margin: 0 0 8px;
    font-size: 16px;
}

.guide-section p {
    max-width: 860px;
    margin: 0 0 12px;
    color: var(--slate);
    line-height: 1.55;
}

.guide-steps,
.guide-columns {
    display: grid;
    gap: 12px;
}

.guide-steps {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.guide-steps div,
.guide-columns div,
.guide-callout {
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fbfcfd;
}

.guide-steps strong {
    width: 30px;
    height: 30px;
    display: inline-grid;
    place-items: center;
    margin-bottom: 10px;
    border-radius: 8px;
    background: var(--green);
    color: #ffffff;
}

.guide-steps span {
    display: block;
    color: var(--slate);
}

.guide-columns {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.guide-list,
.guide-checklist {
    margin: 0;
    padding-left: 20px;
    color: var(--slate);
    line-height: 1.65;
}

.guide-callout {
    display: grid;
    gap: 6px;
    border-color: rgba(192, 57, 43, 0.3);
    background: #fff0ee;
}

.guide-protocol-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px;
    margin: 12px 0;
}

.guide-protocol-grid div {
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fbfcfd;
}

.guide-protocol-grid span {
    display: block;
    color: var(--muted);
    font-size: 12px;
}

.guide-protocol-grid strong {
    display: block;
    margin-top: 5px;
    word-break: break-word;
}

.guide-code {
    margin: 12px 0;
    padding: 14px;
    overflow: auto;
    border-radius: 8px;
    background: #13221f;
    color: #d8fff1;
    font-size: 13px;
    line-height: 1.55;
}

.guide-table-wrap {
    overflow-x: auto;
}

.guide-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
}

.guide-table th,
.guide-table td {
    padding: 11px 10px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}

.guide-table th {
    color: var(--muted);
    font-size: 13px;
}

@media (max-width: 1020px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .sidebar {
        position: fixed;
        z-index: 1000;
        width: 250px;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
    }

    body.sidebar-open .sidebar {
        transform: translateX(0);
    }

    .sidebar-toggle {
        display: inline-flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 5px;
    }

    .dashboard-grid {
        grid-template-columns: 1fr;
    }

    .side-panel {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .guide-layout {
        grid-template-columns: 1fr;
    }

    .guide-toc {
        position: static;
    }
}

@media (max-width: 760px) {
    .main {
        padding: 16px;
    }

    .topbar h1 {
        font-size: 24px;
    }

    .login-page {
        grid-template-columns: 1fr;
    }

    .login-visual {
        min-height: 220px;
        padding: 28px;
    }

    .login-visual h1 {
        font-size: 32px;
    }

    .login-panel {
        padding: 28px;
    }

    .side-panel,
    .form-compact,
    .modal-form-grid {
        grid-template-columns: 1fr;
    }

    .form-compact .wide,
    .modal-form-grid .wide {
        grid-column: auto;
    }

    .device-map-toolbar,
    .modal-header,
    .modal-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .metric-grid,
    .stats-grid,
    .guide-steps,
    .guide-columns,
    .guide-protocol-grid {
        grid-template-columns: 1fr;
    }
}
