:root {
    --bg: #f5f7fa;
    --panel: #ffffff;
    --panel-alt: #edf3f8;
    --text: #172033;
    --muted: #627084;
    --line: #d8e0ea;
    --accent: #256f8f;
    --accent-strong: #174f68;
    --good: #147444;
    --good-bg: #e7f6ee;
    --warn: #9b5d00;
    --warn-bg: #fff4dc;
    --bad: #ad2f2f;
    --bad-bg: #fdeaea;
    --sidebar-bg: #ffffff;
    --sidebar-text: #172033;
    --sidebar-muted: #627084;
    --sidebar-active: rgba(37, 111, 143, 0.12);
    --sidebar-hover-bg: rgba(15, 23, 42, 0.06);
    --sidebar-hover-text: #172033;
    --sidebar-active-text: #174f68;
    --sidebar-status-bg: #edf3f8;
    --sidebar-border: #e6ecf2;
    --row-hover: #e9f4f8;
    --kv-bg: #f8fafc;
    --empty-state-bg: #fbfdff;
    --shadow: 0 12px 28px rgba(23, 32, 51, 0.08);
    --radius: 14px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
}

body {
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: 'Inter', Arial, Helvetica, sans-serif;
    font-size: 15px;
    line-height: 1.45;
}

a {
    color: var(--accent-strong);
    text-decoration: none;
}

a:hover {
    text-decoration: none;
}

h1,
h2,
h3 {
    margin: 0;
    line-height: 1.15;
}

h1 {
    font-size: 32px;
}

h2 {
    font-size: 20px;
}

h3 {
    font-size: 16px;
    margin-bottom: 12px;
}

.skip-link {
    position: absolute;
    left: -9999px;
    top: 0;
}

.skip-link:focus {
    left: 16px;
    top: 16px;
    z-index: 1000;
    background: #ffffff;
    color: #000000;
    padding: 10px 14px;
    border-radius: 10px;
}

.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 260px;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 24px 18px;
    border-right: 1px solid var(--sidebar-border);
    position: sticky;
    top: 0;
    height: 100vh;
}

.sidebar-top {
    display: block;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 28px;
    color: var(--sidebar-text);
}

.brand:hover {
    color: var(--sidebar-text);
}

.brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    background: linear-gradient(135deg, #2d89b0, #174f68);
    display: grid;
    place-items: center;
    font-weight: 700;
    color: #ffffff;
    flex-shrink: 0;
}

.brand-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.brand-text strong {
    font-size: 16px;
    line-height: 1.1;
}

.brand-text small {
    color: var(--sidebar-muted);
    font-size: 13px;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.nav-link {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding: 12px 14px;
    border-radius: 12px;
    color: var(--sidebar-text);
    font-weight: 600;
    border: 1px solid transparent;
    transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

.nav-link:hover {
    background: var(--sidebar-hover-bg);
    color: var(--sidebar-hover-text);
}

.nav-link.is-active {
    background: var(--sidebar-active);
    color: var(--sidebar-active-text);
    border-color: rgba(111, 190, 226, 0.3);
}

.sidebar-bottom {
    margin-top: 24px;
}

.sidebar-status {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px;
    border-radius: 12px;
    background: var(--sidebar-status-bg);
    color: var(--sidebar-muted);
    font-size: 14px;
}

.sidebar-user {
    margin-top: 12px;
    padding: 0 14px;
    color: var(--sidebar-text);
}

.sidebar-user-label {
    display: block;
    margin-bottom: 3px;
    color: var(--sidebar-muted);
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
}

.sidebar-user strong {
    display: block;
    overflow-wrap: anywhere;
    font-size: 14px;
}

.sidebar-logout {
    margin-top: 12px;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 999px;
    background: #22c55e;
    flex-shrink: 0;
}

.main-content {
    flex: 1;
    min-width: 0;
    padding: 32px;
}

.page {
    width: 100%;
    max-width: 1500px;
    margin: 0 auto;
}

body > main.page {
    padding: 32px;
}

.topbar,
.panel-header,
.detail-title {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 18px;
}

.topbar {
    margin-bottom: 22px;
}

.subtitle {
    margin: 8px 0 0;
    color: var(--muted);
    max-width: 850px;
}

.actions,
.form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.actions {
    justify-content: flex-end;
}

.btn,
button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    padding: 8px 14px;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    box-shadow: none;
}

.btn:hover,
button:hover {
    border-color: #bec9d6;
}

.btn-primary,
button.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #ffffff;
}

.btn-danger,
button.btn-danger {
    background: var(--bad);
    border-color: var(--bad);
    color: #ffffff;
}

.notice,
.error {
    margin-bottom: 18px;
    border-radius: 10px;
    padding: 12px 14px;
    font-weight: 700;
}

.notice {
    border: 1px solid #a8d1bd;
    background: var(--good-bg);
    color: #14583a;
}

.error {
    border: 1px solid #efb8b8;
    background: var(--bad-bg);
    color: #7a2020;
}

.summary-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 18px;
}

.metric,
.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.metric {
    padding: 16px;
}

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

.metric strong {
    display: block;
    margin-top: 6px;
    font-size: 24px;
}

.layout {
    display: grid;
    grid-template-columns: minmax(0, 1.45fr) minmax(380px, 0.8fr);
    gap: 18px;
    align-items: start;
}

.panel-header {
    border-bottom: 1px solid var(--line);
    padding: 16px;
}

.panel-body {
    padding: 16px;
}

.filters,
.form-grid,
.kv-grid {
    display: grid;
    gap: 10px;
}

.filters {
    grid-template-columns: minmax(220px, 2fr) repeat(3, minmax(130px, 1fr)) auto;
    align-items: end;
    margin-bottom: 14px;
}

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

.full {
    grid-column: 1 / -1;
}

label {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    margin-bottom: 5px;
    text-transform: uppercase;
}

input,
select,
textarea {
    width: 100%;
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    font: inherit;
    padding: 8px 10px;
}

textarea {
    min-height: 82px;
    resize: vertical;
}

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

table {
    border-collapse: collapse;
    width: 100%;
    min-width: 960px;
}

th,
td {
    border-bottom: 1px solid var(--line);
    padding: 11px 10px;
    text-align: left;
    vertical-align: middle;
    white-space: nowrap;
}

td {
    overflow-wrap: anywhere;
}

th {
    color: var(--muted);
    font-size: 12px;
    text-transform: uppercase;
    background: var(--panel-alt);
}

tbody tr:hover,
.selected-row {
    background: var(--row-hover);
}

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

.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 800;
    padding: 4px 8px;
    border: 1px solid transparent;
}

.badge-good {
    color: var(--good);
    background: var(--good-bg);
    border-color: #b5e2ca;
}

.badge-warn {
    color: var(--warn);
    background: var(--warn-bg);
    border-color: #f2d394;
}

.badge-bad {
    color: var(--bad);
    background: var(--bad-bg);
    border-color: #f4b7b7;
}

.badge-muted {
    color: #516070;
    background: #edf1f5;
    border-color: #d4dde7;
}

.kv {
    background: var(--kv-bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
    min-height: 66px;
    overflow-wrap: anywhere;
}

.kv span:first-child {
    display: block;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    margin-bottom: 5px;
    text-transform: uppercase;
}

.section {
    border-top: 1px solid var(--line);
    padding-top: 16px;
    margin-top: 16px;
}

.list {
    display: grid;
    gap: 8px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.list li {
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px;
    background: var(--panel);
}

.nested-list {
    margin-top: 8px;
}

.section-actions {
    margin-top: 16px;
}

.empty-state {
    border: 1px dashed var(--line);
    border-radius: 12px;
    padding: 22px;
    color: var(--muted);
    text-align: center;
    background: var(--empty-state-bg);
}

.danger-box {
    border: 1px solid #f0b9b9;
    background: var(--bad-bg);
    color: #6e1f1f;
    border-radius: 10px;
    padding: 14px;
    margin-bottom: 16px;
}

@media (max-width: 1120px) {
    .summary-grid {
        grid-template-columns: 1fr 1fr;
    }

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

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

@media (max-width: 900px) {
    .app-shell {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        position: static;
        padding: 16px;
        border-right: 0;
    }

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

    .nav-link {
        min-height: 42px;
        justify-content: center;
        padding: 10px 12px;
        text-align: center;
    }

    .main-content {
        padding: 20px;
    }

    body > main.page {
        padding: 20px;
    }

    .sidebar-bottom {
        margin-top: 16px;
    }
}

@media (max-width: 760px) {
    body {
        font-size: 14px;
    }

    .brand {
        margin-bottom: 16px;
    }

    .topbar,
    .panel-header,
    .detail-title {
        display: block;
    }

    .actions {
        justify-content: flex-start;
        margin-top: 14px;
    }

    .summary-grid,
    .filters,
    .form-grid,
    .kv-grid {
        grid-template-columns: 1fr;
    }

    .panel,
    .metric {
        border-radius: 10px;
    }

    .panel-header,
    .panel-body,
    .metric {
        padding: 14px;
    }

    .table-wrap {
        margin: 0 -14px;
        padding: 0 14px;
    }

    table {
        min-width: 760px;
    }

    th,
    td {
        padding: 10px 8px;
    }

    h1 {
        font-size: 26px;
    }
}

@media (max-width: 560px) {
    .sidebar {
        padding: 14px;
    }

    .brand-mark {
        width: 38px;
        height: 38px;
    }

    .sidebar-nav {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
    }

    .nav-link {
        min-height: 40px;
        border-radius: 10px;
        font-size: 14px;
    }

    .sidebar-status {
        padding: 12px;
    }

    .main-content {
        padding: 16px;
    }

    body > main.page {
        padding: 16px;
    }

    .page {
        max-width: none;
    }

    .topbar {
        margin-bottom: 16px;
    }

    .subtitle {
        max-width: none;
    }

    .actions,
    .form-actions {
        width: 100%;
    }

    .actions .btn,
    .actions button,
    .form-actions .btn,
    .form-actions button {
        flex: 1 1 100%;
    }

    .btn,
    button {
        width: 100%;
        min-height: 44px;
    }

    input,
    select,
    textarea {
        min-height: 44px;
    }

    .summary-grid,
    .layout {
        gap: 12px;
    }

    .metric strong {
        font-size: 22px;
    }

    .empty-state {
        padding: 18px 12px;
    }
}

@media (max-width: 380px) {
    .main-content {
        padding: 12px;
    }

    body > main.page {
        padding: 12px;
    }

    .sidebar {
        padding: 12px;
    }

    .brand-text strong {
        font-size: 15px;
    }

    .brand-text small,
    .sidebar-status {
        font-size: 13px;
    }

    h1 {
        font-size: 24px;
    }

    h2 {
        font-size: 18px;
    }
}

/* ============================================================
 * UI commit 1: inline SVG logo + nav icons
 * Appended for the feat/ui-icons-dark-mode-logo branch.
 * ============================================================ */

/* Brand mark - inline SVG logo replaces the old "K" text mark.
 * The SVG uses currentColor for its black paths, so we control
 * those via the color property. The red accents stay red. */
.brand-mark {
    width: 44px;
    height: 44px;
    background: transparent;
    padding: 0;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: var(--sidebar-text);
    overflow: hidden;
}

.brand-mark svg {
    width: 100%;
    height: 100%;
    display: block;
}

/* Nav icons - 18px square, sit to the left of the label,
 * inherit color from the nav-link so active/hover states work. */
.nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
}

.nav-link .nav-icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    stroke-width: 2;
}

.nav-link.is-active .nav-icon,
.nav-link:hover .nav-icon {
    /* nav-icon inherits color from nav-link, so this is implicit -
     * including the selector here as a comment hook for future tweaks */
}

/* Sign-out link at the bottom of sidebar gets the same icon treatment */
.sidebar-logout {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* ============================================================
 * UI commit 3: dark mode palette + theme toggle
 * ============================================================ */

/* Dark mode palette - applied when <html data-theme="dark"> is set.
 * The dark palette uses the same semantic variable names as :root,
 * so every rule that already says var(--panel) etc adapts automatically.
 *
 * Body bg sits darkest so the sidebar (#0f172a) appears as a slightly
 * lighter panel. Panels sit lighter still, so they stand out on the
 * dark bg as containers. Lines are subtle. Semantic notice backgrounds
 * become translucent tints over the dark surface, so they read as
 * coloured callouts rather than bright boxes.
 */
[data-theme="dark"] {
    --bg: #0a101c;
    --panel: #1a2236;
    --panel-alt: #232c41;
    --text: #e6e9ef;
    --muted: #94a3b8;
    --line: #2d384d;
    --accent: #3091b5;
    --accent-strong: #4dabd0;
    --good: #4ade80;
    --good-bg: rgba(20, 116, 68, 0.22);
    --warn: #fbbf24;
    --warn-bg: rgba(155, 93, 0, 0.22);
    --bad: #f87171;
    --bad-bg: rgba(173, 47, 47, 0.22);
    --row-hover: #232c41;
    --kv-bg: #1a2236;
    --empty-state-bg: #1a2236;
    --shadow: 0 12px 28px rgba(0, 0, 0, 0.5);
    --sidebar-bg: #0f172a;
    --sidebar-text: #cbd5e1;
    --sidebar-muted: #94a3b8;
    --sidebar-active: rgba(37, 111, 143, 0.22);
    --sidebar-hover-bg: rgba(255, 255, 255, 0.06);
    --sidebar-hover-text: #ffffff;
    --sidebar-active-text: #ffffff;
    --sidebar-status-bg: rgba(255, 255, 255, 0.05);
    --sidebar-border: rgba(255, 255, 255, 0.08);
}

/* Smooth the transition when the user toggles. Targets only the body's
 * own properties; child elements that use var(--panel) etc will swap
 * instantly which is what we want (no per-element animation cascade). */
body {
    transition: background 0.18s ease, color 0.18s ease;
}

/* ------------------------------------------------------------
 * Theme toggle button - sits in sidebar-bottom above sign-out.
 * The button uses the .nav-link class for visual parity with
 * the other sidebar items. ------------------------------------- */
.theme-toggle {
    width: 100%;
    background: transparent;
    border: 1px solid transparent;
    font: inherit;
    text-align: left;
    cursor: pointer;
    margin-top: 8px;
}

.theme-toggle:hover {
    background: var(--sidebar-hover-bg);
    color: var(--sidebar-hover-text);
}

/* Sun shows in light mode, moon shows in dark mode */
.theme-icon-dark {
    display: none;
}

[data-theme="dark"] .theme-icon-light {
    display: none;
}

[data-theme="dark"] .theme-icon-dark {
    display: inline-block;
}

/* Same swap for the label text - "Dark mode" prompt in light mode,
 * "Light mode" prompt in dark mode. */
.theme-toggle-label-dark {
    display: none;
}

[data-theme="dark"] .theme-toggle-label-light {
    display: none;
}

[data-theme="dark"] .theme-toggle-label-dark {
    display: inline;
}

/* ============================================================
 * Mobile nav: in-place collapse with hamburger (feat/mobile-a11y)
 * Desktop is unchanged. At 900px and below the sidebar shows a
 * compact bar (logo + hamburger); the rest collapses behind it.
 * ============================================================ */

/* Desktop: hide the mobile bar; the collapse region fills the
 * sidebar and keeps the bottom section pinned down as before. */
.sidebar-mobile-bar {
    display: none;
}

#sidebar-collapse {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1 1 auto;
    min-height: 0;
}

.nav-toggle {
    width: auto;
    min-width: 44px;
    min-height: 44px;
    padding: 8px;
    border: 1px solid var(--sidebar-border);
    border-radius: 10px;
    background: transparent;
    color: var(--sidebar-text);
}

.nav-toggle .nav-icon {
    width: 22px;
    height: 22px;
}

@media (max-width: 900px) {
    /* Show the compact bar (logo + hamburger). */
    .sidebar-mobile-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
    }

    .sidebar-mobile-bar .brand {
        margin-bottom: 0;
    }

    /* Collapse everything else by default; reveal only when open. */
    #sidebar-collapse {
        display: none;
    }

    .sidebar[data-nav-open="true"] #sidebar-collapse {
        display: block;
        margin-top: 14px;
    }

    /* The compact bar already shows the logo, so hide the inner one. */
    #sidebar-collapse .brand {
        display: none;
    }
}

/* ============================================================
 * Accessibility pass (feat/mobile-a11y)
 * Visible keyboard focus + reduced-motion support.
 * ============================================================ */

a:focus-visible,
button:focus-visible,
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
.nav-link:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

[data-theme="dark"] a:focus-visible,
[data-theme="dark"] button:focus-visible,
[data-theme="dark"] .btn:focus-visible,
[data-theme="dark"] input:focus-visible,
[data-theme="dark"] select:focus-visible,
[data-theme="dark"] textarea:focus-visible,
[data-theme="dark"] .nav-link:focus-visible {
    outline-color: #7cc6e3;
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        scroll-behavior: auto !important;
    }
}


/* Mock mode / Live data sidebar-status badge */
.sidebar-status-live .status-dot {
    background-color: #22c55e;
}
.sidebar-status-mock .status-dot {
    background-color: #f59e0b;
}
