:root {
    --ink: #2a2108;
    --ink-soft: #5c4a22;
    --teal: #4a3808;
    --teal-deep: #4a3808;
    --teal-glow: #6e5412;
    --sand: #e4d4b0;
    --sand-soft: #f3ead8;
    --cream: #f8f3e9;
    --surface: rgba(255, 252, 246, 0.90);
    --line: rgba(74, 56, 8, 0.12);
    --danger: #9b2c2c;
    --ok: #3d5c28;
    --shadow: 0 18px 50px rgba(74, 56, 8, 0.14);
    --radius: 18px;
    --font-display: "Instrument Serif", Georgia, serif;
    --font-body: "DM Sans", "Segoe UI", sans-serif;
    --sidebar-w: 260px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
}

html, body {
    margin: 0;
    min-height: 100%;
}

body {
    font-family: var(--font-body);
    color: var(--ink);
    background:
        radial-gradient(1200px 600px at 10% -10%, rgba(74, 56, 8, 0.16), transparent 55%),
        radial-gradient(900px 500px at 100% 0%, rgba(228, 212, 176, 0.55), transparent 50%),
        linear-gradient(165deg, #f3eee4 0%, var(--cream) 45%, #ebe3d4 100%);
}

a {
    color: inherit;
    text-decoration: none;
}

img, svg, video {
    max-width: 100%;
    height: auto;
}

/* ---------- Login ---------- */
.login-body {
    min-height: 100vh;
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: max(1rem, var(--safe-top)) 1rem max(1.5rem, var(--safe-bottom));
    position: relative;
    overflow-x: hidden;
}

.login-body::before {
    content: "";
    position: absolute;
    inset: 0;
    background:
        linear-gradient(120deg, rgba(74, 56, 8, 0.92), rgba(54, 40, 6, 0.62)),
        url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    z-index: 0;
}

.login-panel {
    position: relative;
    z-index: 1;
    width: min(440px, 100%);
    padding: 2.75rem 2.4rem 2.4rem;
    border-radius: 28px;
    background: var(--surface);
    backdrop-filter: blur(16px);
    box-shadow: var(--shadow);
    border: 1px solid rgba(255, 255, 255, 0.55);
    animation: rise 0.55s ease both;
}

@keyframes rise {
    from { opacity: 0; transform: translateY(18px); }
    to { opacity: 1; transform: translateY(0); }
}

.login-brand {
    margin-bottom: 1.75rem;
}

.login-brand .brand-mark {
    display: inline-flex;
    width: 52px;
    height: 52px;
    border-radius: 14px;
    align-items: center;
    justify-content: center;
    background: linear-gradient(145deg, var(--teal), var(--teal-deep));
    color: #fff;
    font-weight: 700;
    letter-spacing: 0.04em;
    margin-bottom: 1rem;
}

.login-brand h1 {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(2.2rem, 8vw, 3.1rem);
    font-weight: 400;
    letter-spacing: -0.02em;
    color: var(--teal-deep);
    line-height: 1;
}

.login-brand p {
    margin: 0.55rem 0 0;
    color: var(--ink-soft);
    font-size: 0.98rem;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 1rem;
}

.field label {
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--ink-soft);
}

.field input,
.field select,
.filters input,
.filters select {
    width: 100%;
    border: 1px solid var(--line);
    background: #fffef9;
    border-radius: 12px;
    padding: 0.85rem 1rem;
    font: inherit;
    font-size: 16px; /* avoid iOS zoom on focus */
    color: var(--ink);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.field input:focus,
.filters input:focus,
.filters select:focus {
    outline: none;
    border-color: var(--teal-glow);
    box-shadow: 0 0 0 3px rgba(74, 56, 8, 0.18);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    border: none;
    border-radius: 12px;
    padding: 0.9rem 1.25rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.15s ease, background 0.2s, box-shadow 0.2s;
    min-height: 44px;
    touch-action: manipulation;
}

.btn:hover {
    transform: translateY(-1px);
}

.btn-primary {
    width: 100%;
    background: linear-gradient(145deg, var(--teal-glow), var(--teal-deep));
    color: #fff;
    box-shadow: 0 10px 24px rgba(74, 56, 8, 0.28);
    margin-top: 0.5rem;
}

.btn-secondary {
    background: var(--sand-soft);
    color: var(--teal-deep);
    border: 1px solid var(--line);
}

.alert {
    border-radius: 12px;
    padding: 0.85rem 1rem;
    margin-bottom: 1rem;
    font-size: 0.95rem;
}

.alert-error {
    background: #fde8e8;
    color: var(--danger);
    border: 1px solid #f5c2c2;
}

.alert-ok {
    background: #e4f5ec;
    color: var(--ok);
    border: 1px solid #b7e0c8;
}

/* ---------- App shell ---------- */
.app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    min-height: 100vh;
    min-height: 100dvh;
}

.sidebar-backdrop {
    display: none;
}

.sidebar {
    background: linear-gradient(180deg, #4a3808 0%, #2f2405 100%);
    color: #eef7f5;
    padding: 1.5rem 1.1rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    position: sticky;
    top: 0;
    height: 100vh;
    height: 100dvh;
    z-index: 40;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.35rem 0.5rem;
}

.sidebar-brand .brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    background: rgba(255, 255, 255, 0.12);
    font-weight: 700;
    flex-shrink: 0;
}

.sidebar-brand strong {
    display: block;
    font-size: 1.05rem;
}

.sidebar-brand small {
    opacity: 0.7;
    font-size: 0.78rem;
}

.nav-close {
    display: none;
    margin-left: auto;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1.75rem;
    line-height: 1;
    cursor: pointer;
    padding: 0.25rem 0.4rem;
    min-width: 44px;
    min-height: 44px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
    overflow-y: auto;
}

.sidebar-nav a {
    padding: 0.75rem 0.9rem;
    border-radius: 10px;
    color: rgba(255, 255, 255, 0.78);
    font-weight: 500;
    transition: background 0.2s, color 0.2s;
    min-height: 44px;
    display: flex;
    align-items: center;
}

.sidebar-nav a:hover,
.sidebar-nav a.active {
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
}

.nav-label {
    margin: 1rem 0.9rem 0.35rem;
    font-size: 0.72rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0.55;
}

.sidebar-foot {
    padding: 0.5rem;
    padding-bottom: max(0.5rem, var(--safe-bottom));
}

.logout-link {
    display: block;
    text-align: center;
    padding: 0.75rem;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
    font-weight: 600;
    min-height: 44px;
}

.main {
    min-width: 0;
    width: 100%;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: max(1rem, var(--safe-top)) 2rem 0.5rem;
    gap: 0.75rem;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}

.topbar h1 {
    margin: 0;
    font-family: var(--font-display);
    font-size: clamp(1.45rem, 4vw, 2.35rem);
    font-weight: 400;
    color: var(--teal-deep);
    line-height: 1.15;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav-toggle {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    cursor: pointer;
    flex-shrink: 0;
}

.nav-toggle span {
    display: block;
    height: 2px;
    width: 100%;
    background: var(--teal-deep);
    border-radius: 2px;
}

.topbar-user {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-shrink: 0;
}

.user-chip,
.type-chip {
    padding: 0.4rem 0.75rem;
    border-radius: 999px;
    font-size: 0.85rem;
    font-weight: 600;
}

.user-chip {
    background: #fff;
    border: 1px solid var(--line);
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.type-chip {
    background: var(--sand);
    color: var(--teal-deep);
}

.content {
    padding: 1rem 2rem calc(2.5rem + var(--safe-bottom));
}

/* ---------- Dashboard / panels ---------- */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.kpi {
    padding: 1.25rem 1.3rem;
    border-radius: var(--radius);
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.65);
    box-shadow: var(--shadow);
    animation: rise 0.5s ease both;
    min-width: 0;
}

.kpi:nth-child(2) { animation-delay: 0.05s; }
.kpi:nth-child(3) { animation-delay: 0.1s; }
.kpi:nth-child(4) { animation-delay: 0.15s; }

.kpi .label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--ink-soft);
    margin-bottom: 0.55rem;
}

.kpi .value {
    font-family: var(--font-display);
    font-size: clamp(1.5rem, 4vw, 2rem);
    color: var(--teal-deep);
    line-height: 1.1;
    word-break: break-word;
}

.kpi .hint {
    margin-top: 0.35rem;
    font-size: 0.85rem;
    color: var(--ink-soft);
}

.panel {
    background: var(--surface);
    border: 1px solid rgba(255, 255, 255, 0.65);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem 1.35rem;
    margin-bottom: 1.25rem;
}

.panel-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.panel-head h2 {
    margin: 0;
    font-size: 1.15rem;
    color: var(--teal-deep);
}

.panel-head p {
    margin: 0;
    color: var(--ink-soft);
    font-size: 0.9rem;
}

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: end;
    margin-bottom: 1rem;
}

.filters .field {
    margin: 0;
    min-width: 140px;
    flex: 1 1 140px;
}

.filters .field.grow {
    flex: 1 1 200px;
    min-width: 160px;
}

.filters .btn,
.filters a.btn {
    flex: 0 0 auto;
    width: auto;
    margin: 0;
}

.table-wrap {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 12px;
    border: 1px solid var(--line);
    max-width: 100%;
}

table.data {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
    background: #fffefb;
    min-width: 640px;
}

table.data th,
table.data td {
    padding: 0.75rem 0.85rem;
    text-align: left;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}

table.data th {
    background: #f0ebe1;
    color: var(--ink-soft);
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    position: sticky;
    top: 0;
    z-index: 1;
}

table.data tbody tr:hover {
    background: #f7faf8;
}

table.data .num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

table.table-clickable tbody tr.click-row {
    cursor: pointer;
}

table.table-clickable tbody tr.click-row:hover,
table.table-clickable tbody tr.click-row:focus {
    background: #f3ead8;
    outline: none;
}

.detail-meta {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}

.detail-meta div {
    background: var(--sand-soft);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.7rem 0.9rem;
}

.detail-meta span {
    display: block;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--ink-soft);
    margin-bottom: 0.2rem;
}

.detail-meta strong {
    font-size: 0.98rem;
    color: var(--teal-deep);
    word-break: break-word;
}

.totals {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-top: 1rem;
}

.total-pill {
    background: var(--sand-soft);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 0.7rem 1rem;
    flex: 1 1 140px;
}

.total-pill span {
    display: block;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--ink-soft);
}

.total-pill strong {
    font-family: var(--font-display);
    font-size: 1.35rem;
    color: var(--teal-deep);
}

.empty {
    padding: 2rem 1rem;
    text-align: center;
    color: var(--ink-soft);
}

.mode-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.mode-tabs button {
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 999px;
    padding: 0.55rem 0.95rem;
    font: inherit;
    cursor: pointer;
    min-height: 44px;
}

.mode-tabs button.active {
    background: var(--teal);
    color: #fff;
    border-color: var(--teal);
}

.chart-row {
    display: grid;
    grid-template-columns: 1.2fr 1fr;
    gap: 1rem;
}

.bar-chart {
    display: flex;
    align-items: flex-end;
    gap: 0.35rem;
    height: 180px;
    padding: 0.5rem 0.25rem 1.6rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.bar-chart .bar {
    flex: 1 0 auto;
    min-width: 16px;
    max-width: 48px;
    background: linear-gradient(180deg, var(--teal-glow), var(--teal-deep));
    border-radius: 8px 8px 4px 4px;
    position: relative;
    opacity: 0.9;
    transition: opacity 0.2s;
}

.bar-chart .bar:hover {
    opacity: 1;
}

.bar-chart .bar span {
    position: absolute;
    bottom: -1.4rem;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.65rem;
    color: var(--ink-soft);
    white-space: nowrap;
}

/* ---------- Tablet ---------- */
@media (max-width: 1100px) {
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
    .chart-row { grid-template-columns: 1fr; }
    .content, .topbar { padding-left: 1.25rem; padding-right: 1.25rem; }
}

/* ---------- Mobile drawer ---------- */
@media (max-width: 860px) {
    .app-shell {
        grid-template-columns: 1fr;
    }

    .nav-toggle {
        display: inline-flex;
    }

    .nav-close {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(47, 36, 5, 0.45);
        z-index: 45;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
    }

    .sidebar-backdrop.is-open {
        opacity: 1;
        pointer-events: auto;
    }

    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(var(--sidebar-w), 86vw);
        height: 100vh;
        height: 100dvh;
        transform: translateX(-105%);
        transition: transform 0.28s ease;
        box-shadow: none;
        padding-top: max(1.25rem, var(--safe-top));
    }

    .sidebar.is-open {
        transform: translateX(0);
        box-shadow: 16px 0 40px rgba(0, 0, 0, 0.25);
    }

    body.nav-open {
        overflow: hidden;
    }

    .topbar {
        padding-top: max(0.85rem, var(--safe-top));
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .content {
        padding: 0.75rem 1rem calc(1.75rem + var(--safe-bottom));
    }

    .type-chip {
        display: none;
    }

    .panel {
        padding: 1rem;
        border-radius: 14px;
    }

    .filters {
        flex-direction: column;
        align-items: stretch;
    }

    .filters .field,
    .filters .field.grow {
        min-width: 0;
        width: 100%;
        flex: 1 1 auto;
    }

    .filters .btn,
    .filters a.btn {
        width: 100%;
    }
}

/* ---------- Small phones ---------- */
@media (max-width: 560px) {
    .login-panel {
        padding: 1.75rem 1.25rem 1.5rem;
        border-radius: 20px;
    }

    .login-brand p {
        font-size: 0.9rem;
    }

    .kpi-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .kpi {
        padding: 1rem 1.1rem;
    }

    .panel-head {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
    }

    table.data {
        font-size: 0.85rem;
        min-width: 560px;
    }

    table.data th,
    table.data td {
        padding: 0.65rem 0.7rem;
    }

    .user-chip {
        max-width: 90px;
        font-size: 0.78rem;
        padding: 0.35rem 0.6rem;
    }

    .bar-chart {
        height: 150px;
    }
}
