:root {
    --bg: #f2f6fa; --card: #ffffff; --ink: #122033; --muted: #5f7081; --line: #e0e8ef;
    --brand: #0b6fa4; --brand2: #14a3c9; --brand-dark: #08557f;
    --side: #0a2133; --side-ink: #bdd3e4;
    --ok: #1f8a4c; --warn: #b7791f; --bad: #c0392b; --bar: #0b6fa4;
    --shadow: 0 1px 2px rgba(16, 32, 51, .06), 0 6px 18px rgba(16, 32, 51, .06);
    --r: 14px; --topbar-h: 60px; --tab-h: 64px;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0d1823; --card: #14232f; --ink: #e7eef5; --muted: #93a6b6; --line: #25384a; --side: #08141e; --bar: #4fb0e6;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
    }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); }
a { color: var(--brand); }
h1 { font-size: 1.55rem; margin: 0 0 1rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.ico { flex-shrink: 0; }

/* ---------- Top bar ---------- */
.topbar {
    display: flex; align-items: center; gap: 12px; height: var(--topbar-h); padding: 0 16px;
    background: linear-gradient(135deg, var(--brand) 0%, var(--brand2) 100%); color: #fff;
    position: sticky; top: 0; z-index: 30; box-shadow: 0 2px 10px rgba(8, 40, 64, .25);
}
.icon-btn {
    display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border: 0; border-radius: 12px;
    background: rgba(255, 255, 255, .16); color: #fff; cursor: pointer; padding: 0;
}
.icon-btn:hover { background: rgba(255, 255, 255, .26); }
.menu-btn { display: none; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; color: #fff; text-decoration: none; }
.logo { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; border-radius: 12px; background: rgba(255, 255, 255, .2); flex-shrink: 0; }
.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.brand-text b { font-size: 1.02rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-text small { font-size: .76rem; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }

.usermenu { position: relative; }
.avatar-btn {
    display: flex; align-items: center; gap: 9px; border: 0; border-radius: 99px; padding: 4px 12px 4px 4px;
    background: rgba(255, 255, 255, .16); color: #fff; cursor: pointer; font: inherit;
}
.avatar-btn:hover { background: rgba(255, 255, 255, .26); }
.avatar {
    display: inline-flex; align-items: center; justify-content: center; width: 34px; height: 34px; border-radius: 50%;
    background: #fff; color: var(--brand); font-weight: 700; font-size: .82rem; letter-spacing: .02em; flex-shrink: 0;
}
.avatar.lg { width: 42px; height: 42px; font-size: .95rem; background: linear-gradient(135deg, var(--brand), var(--brand2)); color: #fff; }
.uinfo { display: flex; flex-direction: column; text-align: left; line-height: 1.15; }
.uinfo b { font-size: .88rem; max-width: 160px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.uinfo small { font-size: .72rem; opacity: .85; }
.chev { display: inline-flex; opacity: .8; }

.dropdown {
    position: absolute; right: 0; top: calc(100% + 10px); width: 270px; background: var(--card); color: var(--ink);
    border: 1px solid var(--line); border-radius: var(--r); box-shadow: 0 12px 34px rgba(8, 24, 40, .28); padding: 8px; z-index: 60;
}
.dropdown[hidden] { display: none; }
.dd-head { display: flex; align-items: center; gap: 12px; padding: 8px 8px 12px; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
.dd-branch { padding: 6px 8px 10px; }
.dd-branch select { margin-top: 4px; }
.dd-item { display: flex; align-items: center; gap: 12px; padding: 11px 10px; border-radius: 10px; color: var(--ink); text-decoration: none; }
.dd-item:hover { background: rgba(127, 140, 160, .14); }
.dd-item.danger { color: var(--bad); }

/* ---------- Layout ---------- */
.shell { display: flex; min-height: calc(100vh - var(--topbar-h)); }
.sidebar {
    width: 236px; flex-shrink: 0; background: var(--side); padding: 10px 10px 18px; position: sticky; top: var(--topbar-h);
    align-self: flex-start; height: calc(100vh - var(--topbar-h)); overflow-y: auto;
}
.sidebar a, .sidebar .soon {
    display: flex; align-items: center; gap: 12px; padding: 10px 12px; margin: 2px 0; border-radius: 10px; color: var(--side-ink); text-decoration: none; font-size: .95rem;
}
.sidebar a:hover { background: rgba(255, 255, 255, .07); color: #fff; }
.sidebar a.active { background: linear-gradient(135deg, var(--brand), var(--brand2)); color: #fff; box-shadow: 0 4px 12px rgba(11, 111, 164, .4); }
.sidebar .soon { opacity: .45; cursor: default; }
.nav-group { padding: 16px 12px 5px; font-size: .7rem; letter-spacing: .09em; text-transform: uppercase; color: #6f95b1; }
.drawer-head, .logout-link { display: none; }
.scrim { display: none; }
.content { flex: 1; padding: 22px; min-width: 0; max-width: 1120px; }
.tabbar { display: none; }

/* ---------- Components ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.grid .card { margin-bottom: 0; }
.grid-wide { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.stat { font-size: 1.8rem; font-weight: 700; letter-spacing: -.01em; }
.muted { color: var(--muted); }
.small, small { font-size: .85rem; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 18px; border: 1px solid var(--brand);
    background: linear-gradient(135deg, var(--brand), var(--brand2)); color: #fff; border-radius: 10px; cursor: pointer; font: inherit;
    font-weight: 600; text-decoration: none; transition: filter .15s, transform .05s;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { transform: translateY(1px); }
.btn-ghost { background: transparent; color: inherit; border-color: var(--line); font-weight: 500; }
.btn-ghost:hover { background: rgba(127, 140, 160, .13); filter: none; }
.btn-danger { background: var(--bad); border-color: var(--bad); }
.btn-sm { padding: 5px 12px; font-size: .85rem; border-radius: 9px; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: .83rem; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
input[type=text], input[type=number], input[type=password], input[type=email], input[type=tel], input[type=date], input[type=month], input[type=datetime-local], select, textarea {
    width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; font: inherit; background: var(--card); color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(20, 163, 201, .45); outline-offset: 1px; border-color: var(--brand2); }
.form-grid { display: grid; gap: 0 16px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.check { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.check input { width: auto; }
.alert { padding: 11px 14px; border-radius: 12px; margin-bottom: 14px; border: 1px solid; }
.alert-ok { background: #e6f5ec; border-color: #9fd5b4; color: #145c33; }
.alert-error { background: #fbe9e7; border-color: #efb3ab; color: #8a2417; }
.alert-warn { background: #fdf3e0; border-color: #ecc98a; color: #7a5212; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: .76rem; font-weight: 600; background: var(--line); }
.badge-ok { background: #d5efdf; color: #145c33; }
.badge-off { background: #f6dcd8; color: #8a2417; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
form.inline { display: inline; }
.chip { background: rgba(255, 255, 255, .18); padding: 3px 10px; border-radius: 99px; font-size: .85rem; }
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--bg), rgba(20, 163, 201, .12)); }
.auth-card { width: 100%; max-width: 390px; padding: 26px; }
.auth-logo { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.auth-logo .logo { background: linear-gradient(135deg, var(--brand), var(--brand2)); color: #fff; }
.auth-logo h1 { margin: 0; font-size: 1.35rem; }

/* ---------- Idle warning ---------- */
.idle-overlay { position: fixed; inset: 0; z-index: 200; background: rgba(8, 20, 31, .7); display: flex; align-items: center; justify-content: center; padding: 20px; }
.idle-overlay[hidden] { display: none; }
.idle-box { background: var(--card); color: var(--ink); border-radius: 18px; padding: 24px; max-width: 360px; width: 100%; text-align: center; box-shadow: 0 20px 50px rgba(0, 0, 0, .4); }
.idle-box h2 { margin-bottom: 6px; }
.idle-count { font-size: 2.6rem; font-weight: 700; color: var(--brand); line-height: 1.1; margin: 8px 0 14px; }

/* ---------- Phones and small tablets ---------- */
@media (max-width: 800px) {
    :root { --topbar-h: 58px; }
    body.has-nav { padding-bottom: 0; }
    .topbar { padding: 0 12px; gap: 10px; padding-top: env(safe-area-inset-top); height: calc(var(--topbar-h) + env(safe-area-inset-top)); }
    .menu-btn { display: inline-flex; }
    .uinfo, .chev { display: none; }
    .avatar-btn { padding: 3px; background: transparent; }
    .avatar { width: 38px; height: 38px; }
    .dropdown { position: fixed; left: 12px; right: 12px; top: calc(var(--topbar-h) + env(safe-area-inset-top) + 8px); width: auto; }

    .scrim { display: block; position: fixed; inset: 0; z-index: 90; background: rgba(8, 20, 31, .55); opacity: 0; pointer-events: none; transition: opacity .2s; }
    body.nav-open .scrim { opacity: 1; pointer-events: auto; }
    body.nav-open { overflow: hidden; }
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; width: min(84vw, 320px); z-index: 100; transform: translateX(-102%);
        transition: transform .22s ease; padding: env(safe-area-inset-top) 10px calc(90px + env(safe-area-inset-bottom));
        /* The menu is exactly as tall as the visible screen and scrolls inside itself. */
        align-self: auto; height: 100vh; height: 100dvh; max-height: 100dvh;
        overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; touch-action: pan-y;
    }
    body.nav-open .sidebar { transform: translateX(0); box-shadow: 8px 0 30px rgba(0, 0, 0, .35); }
    .drawer-head { display: flex; align-items: center; gap: 12px; padding: 18px 8px 14px; margin-bottom: 4px; border-bottom: 1px solid rgba(255, 255, 255, .1); color: #fff; }
    .dh-text { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.25; }
    .dh-text b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .dh-text small { opacity: .7; font-size: .78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .drawer-head .icon-btn { background: rgba(255, 255, 255, .1); }
    .sidebar a, .sidebar .soon { padding: 12px; font-size: 1rem; }
    .logout-link { display: flex; margin-top: 14px; color: #ff9a8c !important; border-top: 1px solid rgba(255, 255, 255, .1); border-radius: 0 !important; padding-top: 16px !important; }

    .content { padding: 16px 14px calc(var(--tab-h) + 26px + env(safe-area-inset-bottom)); }
    h1 { font-size: 1.3rem; margin-bottom: .8rem; }
    .card { padding: 14px; border-radius: 16px; margin-bottom: 12px; }
    .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .grid-wide { grid-template-columns: 1fr; }
    .stat { font-size: 1.3rem; word-break: break-word; }
    .stat small { font-size: .7rem; }
    th, td { padding: 8px 8px; font-size: .9rem; }
    .form-grid { grid-template-columns: 1fr; }
    input[type=text], input[type=number], input[type=password], input[type=email], input[type=tel], input[type=date], input[type=month], input[type=datetime-local], select, textarea { font-size: 16px; min-height: 44px; }
    .btn { min-height: 44px; padding: 10px 18px; }
    .btn-sm { min-height: 36px; padding: 6px 12px; }
    .actions > .btn, .actions > button { flex: 0 1 auto; }

    .tabbar {
        display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; height: calc(var(--tab-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
        background: var(--card); border-top: 1px solid var(--line); box-shadow: 0 -4px 18px rgba(16, 32, 51, .1);
    }
    .tab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; border: 0; background: none; color: var(--muted); text-decoration: none; font: inherit; padding: 6px 2px; cursor: pointer; min-width: 0; }
    .tab .tl { font-size: .7rem; font-weight: 600; }
    .tab.on { color: var(--brand); }
    .tab.on .ti { background: rgba(20, 163, 201, .15); }
    .tab .ti { display: inline-flex; align-items: center; justify-content: center; width: 46px; height: 28px; border-radius: 14px; transition: background .15s; }
    .tab.primary .ti {
        width: 54px; height: 54px; border-radius: 50%; margin-top: -26px; background: linear-gradient(135deg, var(--brand), var(--brand2)); color: #fff;
        box-shadow: 0 6px 16px rgba(11, 111, 164, .45); border: 4px solid var(--card);
    }
    .tab.primary.on .ti { background: linear-gradient(135deg, var(--brand), var(--brand2)); }
    .tab.primary .tl { color: var(--brand); margin-top: -2px; }
}
@media (max-width: 340px) {
    .brand-text small { display: none; }
    .tab .tl { font-size: .64rem; }
}
@media print { .topbar, .tabbar, .sidebar, .scrim, .idle-overlay { display: none !important; } }
