:root {
    --bg: #f3f5f7;
    --surface: #ffffff;
    --text: #1a2332;
    --muted: #5c6b7a;
    --line: #d9e0e7;
    --accent: #163a4a;
    --accent-hover: #0f2c38;
    --danger: #9b2c2c;
    --warning: #8a5a12;
    --success: #1f6b43;
    --info: #215f8a;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
    --radius: 10px;
    --control-radius: 8px;
    --font: "Segoe UI", system-ui, -apple-system, sans-serif;
    --sidebar-width: 16rem;
    --topbar-height: 64px;
    --content-max: 1120px;
    --z-dropdown: 20;
    --z-topbar: 30;
    --z-sidebar-overlay: 40;
    --z-sidebar: 50;
    --z-modal: 70;
    --z-toast: 80;
    --z-loader: 90;
    --focus-ring: 2px solid var(--accent);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    line-height: 1.5;
    min-height: 100%;
}

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

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

.container {
    width: min(1120px, calc(100% - 2rem));
    margin: 0 auto;
}

.site-header {
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.header-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    min-height: 64px;
}

.header-nav {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem 1rem;
}

.header-nav form { margin: 0; }

.lede {
    color: var(--muted);
    margin: 0 0 1.25rem;
    max-width: 42rem;
}

.auth-links {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem 1rem;
    margin: 1rem 0 0;
    font-size: 0.9rem;
}

.brand {
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--accent);
    text-decoration: none;
}

.site-main { flex: 1; padding: 2.5rem 0 3rem; }

.site-footer {
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 0.875rem;
    padding: 1.25rem 0;
}

.hero h1, .page-block h1 {
    margin: 0 0 0.75rem;
    font-size: clamp(1.8rem, 4vw, 2.6rem);
    letter-spacing: -0.03em;
}

.eyebrow {
    margin: 0 0 0.5rem;
    color: var(--muted);
    font-size: 0.8rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

.page-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 1.5rem;
}

.panel-form {
    max-width: 28rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 1.25rem;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    border: 1px solid transparent;
    border-radius: var(--control-radius);
    padding: 0.55rem 1rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    background: var(--accent);
    color: #fff;
}

.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn:hover { background: var(--accent-hover); text-decoration: none; color: #fff; }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--line); }
.btn-secondary:hover { background: #eef2f5; color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--line); }
.icon-btn {
    border: 0;
    background: transparent;
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    color: var(--muted);
}

.field { display: grid; gap: 0.35rem; margin-bottom: 1rem; }
.field-label { font-size: 0.875rem; font-weight: 600; }
.field-error { color: var(--danger); font-size: 0.8rem; }

.input {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--control-radius);
    padding: 0.6rem 0.75rem;
    font: inherit;
    background: var(--surface);
    color: var(--text);
}

.input.is-invalid { border-color: var(--danger); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 0.75rem 1rem; border-bottom: 1px solid var(--line); }
.table th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }

.badge {
    display: inline-flex;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    background: #e8eef2;
    color: var(--text);
}
.badge-success { background: #e3f5eb; color: var(--success); }
.badge-warning { background: #fff3de; color: var(--warning); }
.badge-danger { background: #fdecec; color: var(--danger); }

.pagination { display: flex; align-items: center; gap: 0.75rem; }
.pagination-status { color: var(--muted); font-size: 0.875rem; }

.empty-state {
    background: var(--surface);
    border: 1px dashed var(--line);
    border-radius: var(--radius);
    padding: 2rem;
    text-align: center;
}

.dropdown { position: relative; display: inline-block; }
.dropdown-menu {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    min-width: 12rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 0.35rem;
    z-index: var(--z-dropdown);
}
.dropdown-menu a { display: block; padding: 0.45rem 0.7rem; color: var(--text); }

.toast-root {
    position: fixed;
    right: 1rem;
    top: 1rem;
    display: grid;
    gap: 0.5rem;
    z-index: var(--z-toast);
    width: min(22rem, calc(100% - 2rem));
}

.toast {
    background: var(--surface);
    border: 1px solid var(--line);
    border-left: 4px solid var(--info);
    border-radius: 8px;
    padding: 0.8rem 1rem;
    box-shadow: var(--shadow);
}

.toast-success { border-left-color: var(--success); }
.toast-error { border-left-color: var(--danger); }
.toast-warning { border-left-color: var(--warning); }

.modal-root[hidden],
.loader-root[hidden] {
    display: none;
}

.modal-root {
    position: fixed;
    inset: 0;
    z-index: var(--z-modal);
    display: grid;
    place-items: center;
    padding: 1rem;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(16, 24, 40, 0.45);
}

.modal {
    position: relative;
    width: min(32rem, 100%);
    background: var(--surface);
    border-radius: 12px;
    border: 1px solid var(--line);
    padding: 1.25rem;
}

.modal-header, .modal-footer { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.modal-title { margin: 0; font-size: 1.1rem; }
.modal-body { margin: 1rem 0; color: var(--muted); }
.modal-footer { justify-content: flex-end; }

.loader-root {
    position: fixed;
    inset: 0;
    background: rgba(243, 245, 247, 0.72);
    display: grid;
    place-items: center;
    z-index: var(--z-loader);
}

.loader-panel {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 0.9rem 1.1rem;
}

.spinner {
    width: 1.15rem;
    height: 1.15rem;
    border: 2px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.debug-trace {
    background: #111827;
    color: #e5e7eb;
    padding: 1rem;
    overflow: auto;
    border-radius: 8px;
    font-size: 0.8rem;
}

@media (max-width: 640px) {
    .site-main { padding-top: 1.5rem; }
    .toast-root { top: auto; bottom: 1rem; }
}

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: var(--focus-ring);
    outline-offset: 2px;
}

.skip-link {
    position: absolute;
    left: 0.75rem;
    top: -3rem;
    z-index: 100;
    background: var(--accent);
    color: #fff;
    padding: 0.5rem 0.85rem;
    border-radius: var(--control-radius);
}
.skip-link:focus {
    top: 0.75rem;
}

.app-body {
    height: 100vh;
    overflow: hidden;
    background: var(--bg);
}

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

.app-sidebar {
    width: var(--sidebar-width);
    flex: 0 0 var(--sidebar-width);
    background: var(--surface);
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    min-height: 0;
    z-index: var(--z-sidebar);
}

.sidebar-brand {
    height: var(--topbar-height);
    display: flex;
    align-items: center;
    padding: 0 1.25rem;
    border-bottom: 1px solid var(--line);
    flex: 0 0 var(--topbar-height);
}

.sidebar-nav {
    overflow-y: auto;
    padding: 1rem 0.75rem 1.5rem;
}

.sidebar-group { margin-bottom: 1.25rem; }

.sidebar-group-label {
    margin: 0 0 0.4rem 0.65rem;
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
}

.sidebar-item {
    display: block;
    padding: 0.5rem 0.75rem;
    border-radius: var(--control-radius);
    color: var(--text);
    text-decoration: none;
    border-left: 3px solid transparent;
}
.sidebar-item:hover {
    background: #eef2f5;
    text-decoration: none;
}
.sidebar-item.is-active {
    background: #e8eef2;
    border-left-color: var(--accent);
    font-weight: 600;
}

.app-sidebar-overlay {
    display: none;
}

.app-column {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}

.app-topbar {
    height: var(--topbar-height);
    flex: 0 0 var(--topbar-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0 1.25rem;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    z-index: var(--z-topbar);
}

.topbar-start, .topbar-end {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}

.topbar-menu-btn {
    display: none;
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--control-radius);
    padding: 0.4rem 0.75rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.topbar-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-company {
    max-width: 24ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--muted);
    font-size: 0.875rem;
    text-decoration: none;
}
.topbar-company:hover { color: var(--text); }

.app-content {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

.app-content-inner {
    width: min(var(--content-max), calc(100% - 2rem));
    margin: 0 auto;
    padding: 2.5rem 0 3rem;
}

.app-content .page-block > h1,
.app-content .page-header h1 {
    font-size: 1.5rem;
    letter-spacing: -0.02em;
}

.app-content h2 { font-size: 1.25rem; }
.app-content h3 { font-size: 1.0625rem; }

.app-footer {
    flex: 0 0 auto;
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 0.875rem;
    padding: 1.25rem 0;
    width: min(var(--content-max), calc(100% - 2rem));
    margin: 0 auto;
}

.dropdown-menu-end { left: auto; right: 0; }
.dropdown-meta {
    margin: 0;
    padding: 0.45rem 0.7rem 0.25rem;
    color: var(--muted);
    font-size: 0.8rem;
    overflow: hidden;
    text-overflow: ellipsis;
}
.dropdown-action {
    display: block;
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    font: inherit;
    color: var(--text);
    padding: 0.45rem 0.7rem;
    cursor: pointer;
    border-radius: var(--control-radius);
}
.dropdown-action:hover { background: #eef2f5; }

.breadcrumb { margin: 0 0 0.75rem; }
.breadcrumb-list {
    list-style: none;
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem 0.5rem;
    margin: 0;
    padding: 0;
    color: var(--muted);
    font-size: 0.875rem;
}
.breadcrumb-list li:not(:last-child)::after {
    content: "/";
    margin-left: 0.5rem;
    color: var(--line);
}

body.app-sidebar-lock { overflow: hidden; }

@media (max-width: 1199px) {
    .app-sidebar {
        --sidebar-width: 13.75rem;
        width: var(--sidebar-width);
        flex-basis: var(--sidebar-width);
    }
}

@media (max-width: 767px) {
    .topbar-menu-btn { display: inline-flex; }
    .app-sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: min(280px, 86vw);
        flex-basis: auto;
        transform: translateX(-105%);
        box-shadow: none;
        transition: transform 0.2s ease;
    }
    .app-sidebar.is-open { transform: translateX(0); }
    .app-sidebar-overlay {
        display: block;
        position: fixed;
        inset: 0;
        border: 0;
        padding: 0;
        background: rgba(16, 24, 40, 0.45);
        z-index: var(--z-sidebar-overlay);
    }
    .app-sidebar-overlay[hidden] { display: none; }
    .app-content-inner { padding: 1.5rem 0 2rem; }
}

@media (prefers-reduced-motion: reduce) {
    .spinner { animation: none; }
    .app-sidebar { transition: none; }
}
