:root {
    color-scheme: light;
    --canvas: #f5f7fa;
    --surface: #fff;
    --line: #dce2e8;
    --text: #17212b;
    --muted: #667585;
    --brand: #1677ff;
    --brand-dark: #0f5ec7;
    --danger: #d64545;
    --sidebar: #17212b;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--canvas); color: var(--text); font: 14px/1.5 Inter, "Segoe UI", Arial, sans-serif; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; -webkit-tap-highlight-color: transparent; }

/* Shell and navigation */
.app-shell { min-height: 100vh; display: grid; grid-template-columns: 236px minmax(0, 1fr); transition: grid-template-columns .2s ease; }
.sidebar { position: sticky; top: 0; height: 100vh; overflow-y: auto; padding: 20px 14px; background: var(--sidebar); color: #d9e2ec; }
.sidebar-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 24px; }
.brand { display: flex; min-width: 0; align-items: center; gap: 10px; margin-left: 8px; color: #fff; font-size: 16px; font-weight: 700; }
.brand small { display: block; color: #94a3b8; font-size: 11px; font-weight: 400; }
.brand-copy { min-width: 0; overflow: hidden; white-space: nowrap; }
.brand-mark { display: grid; width: 32px; height: 32px; place-items: center; border-radius: 7px; background: var(--brand); font-size: 12px; }
.sidebar-toggle { display: grid; width: 32px; height: 32px; flex: 0 0 auto; place-items: center; padding: 0; border: 0; border-radius: 5px; background: transparent; color: #b4c0cc; cursor: pointer; font-size: 18px; }
.mobile-sidebar-close { display: none; width: 32px; height: 32px; flex: 0 0 auto; place-items: center; padding: 0; border: 0; border-radius: 5px; background: transparent; color: #b4c0cc; cursor: pointer; font-size: 24px; line-height: 1; }
.sidebar-toggle:hover, .mobile-sidebar-close:hover { background: rgba(255,255,255,.11); color: #fff; }
.nav-list { display: grid; gap: 3px; }
.nav-list a { min-width: 0; padding: 8px 10px; border-radius: 5px; color: #b4c0cc; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-list a:hover, .nav-list a.is-active { color: #fff; background: rgba(255,255,255,.11); }
.logout-form { margin: 20px 8px 0; }
.logout-form .button { display: flex; width: 100%; justify-content: flex-start; }
.logout-icon { display: inline-block; width: 18px; text-align: center; }
.mobile-menu-toggle, .mobile-nav-backdrop { display: none; }

/* Page frame */
.main-content { min-width: 0; padding: 0 28px 36px; }
.topbar { min-height: 48px; display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; border-bottom: 1px solid var(--line); }
.topbar-title { min-width: 0; margin: 0; overflow: hidden; color: var(--text); font-size: 16px; font-weight: 650; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; min-width: 0; align-items: center; gap: 14px; }
.current-chat-display { display: flex; min-width: 0; align-items: baseline; gap: 6px; }
.current-chat-display .current-chat-label { flex: 0 0 auto; color: var(--muted); font-size: 12px; font-weight: 400; }
.current-chat-display strong { max-width: 240px; overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.current-chat-display code { flex: 0 0 auto; color: var(--muted); font-size: 12px; }
.eyebrow { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.metric-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.metric-grid article { padding: 16px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); }
.metric-grid span { display: block; color: var(--muted); font-size: 12px; }
.metric-grid strong { display: block; margin-top: 7px; font-size: 24px; }
.content-block { margin-top: 16px; padding: 18px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); }
.content-block > div > h2 { display: none; }

/* Shared table and action styles */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; white-space: nowrap; }
th, td { padding: 10px 9px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); font-size: 12px; font-weight: 600; }
tr:last-child td { border-bottom: 0; }
.table-wrap .button { height: 26px; min-height: 26px; padding: 3px 8px; font-size: 12px; line-height: 1.2; white-space: nowrap; }
.button { --button-hover-background: var(--brand-dark); display: inline-flex; min-height: 34px; align-items: center; justify-content: center; gap: 6px; padding: 7px 11px; border: 1px solid transparent; border-radius: 5px; background: var(--brand); color: #fff; cursor: pointer; }
.button:hover:not(:disabled) { background: var(--button-hover-background); }
.button-secondary { --button-hover-background: #eff4f8; border-color: var(--line); background: #fff; color: var(--text); }
.button-danger { --button-hover-background: #b63333; background: var(--danger); }
.button-quiet { --button-hover-background: rgba(255,255,255,.11); padding: 6px 0; background: transparent; color: #b4c0cc; }
.button-quiet:hover:not(:disabled) { color: #fff; }
.button-primary { width: 100%; margin-top: 4px; }
.button-small { min-height: 30px; padding: 5px 8px; font-size: 12px; }
.badge { display: inline-block; padding: 2px 7px; border-radius: 999px; background: #e8edf2; color: #455766; font-size: 12px; }
.badge-good { background: #def3e7; color: #187449; }
.badge-muted { background: #e8edf2; color: #455766; }
.inline-form { display: flex; align-items: center; gap: 6px; }
.inline-form input { width: 88px; }
.pager { display: flex; align-items: center; justify-content: flex-end; gap: 10px; margin-top: 14px; color: var(--muted); }

/* Forms and dialogs */
label { color: #425466; font-size: 12px; font-weight: 600; }
input, select, textarea { width: 100%; min-height: 34px; padding: 7px 8px; border: 1px solid #bdc9d4; border-radius: 4px; background: #fff; color: var(--text); transition: none; }
input[type="checkbox"], input[type="radio"] { width: auto; min-height: auto; accent-color: var(--brand); }
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: none; }
.grid-form { display: grid; gap: 12px; }
.grid-form label { display: grid; gap: 5px; }
.admin-validation-field { position: relative; }
.admin-field-error { position: absolute; top: 0; right: 0; max-width: 72%; overflow: hidden; color: #b63333; font-size: 11px; font-weight: 400; line-height: 1.35; text-align: right; text-overflow: ellipsis; white-space: nowrap; }
.grid-form [aria-invalid="true"] { border-color: #cf5353; background: #fffafa; }
.admin-form-error { padding: 8px 10px; border-left: 3px solid #cf5353; background: #fff4f4; color: #9f2f2f; font-size: 12px; line-height: 1.5; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.dialog-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 4px; }
dialog { width: min(680px, calc(100vw - 30px)); max-height: calc(100vh - 30px); overflow: auto; border: 1px solid var(--line); border-radius: 7px; box-shadow: 0 18px 56px rgba(20,35,50,.24); }
dialog:not([open]) { display: none; }
dialog::backdrop { background: rgba(17, 29, 40, .45); }
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: var(--canvas); }
.login-panel { width: min(390px, 100%); padding: 28px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: 0 16px 50px rgba(20,35,50,.09); }
.login-panel h1 { margin: 3px 0 7px; font-size: 22px; }
.login-panel p { margin: 0 0 20px; color: var(--muted); }
.stack-form { display: grid; gap: 13px; }
.stack-form label { display: grid; gap: 5px; }

/* Toast notifications */
.toast-stack { position: fixed; right: 22px; bottom: 22px; z-index: 1000; display: grid; width: min(390px, calc(100vw - 30px)); gap: 10px; pointer-events: none; }
.toast { display: flex; align-items: flex-start; gap: 12px; padding: 12px 12px 12px 14px; border: 1px solid; border-radius: 7px; background: var(--surface); box-shadow: 0 10px 30px rgba(20,35,50,.18); pointer-events: auto; transition: opacity .18s ease, transform .18s ease; }
.toast-success { border-color: #16834d; background: #16834d; color: #fff; }
.toast-error { border-color: #c83f3f; background: #c83f3f; color: #fff; }
.toast-message { min-width: 0; flex: 1; overflow-wrap: anywhere; }
.toast-close { flex: 0 0 auto; width: 24px; height: 24px; padding: 0; border: 0; border-radius: 4px; background: transparent; color: currentColor; cursor: pointer; font-size: 20px; line-height: 1; }
.toast-close:hover { background: rgba(0,0,0,.07); }
.toast.is-dismissing { opacity: 0; transform: translateY(8px); }

/* Module toolbars */
.module-toolbar { display: flex; align-items: center; justify-content: flex-end; gap: 7px; margin-bottom: 14px; }
.module-toolbar .button-add { flex: 0 0 auto; }
.button-add { --button-hover-background: #b97e0c; border-color: #d8b24c; background: #d99b19; color: #fff; }
.button-add:disabled { border-color: #d8dde3; background: #e8edf2; color: #99a6b2; cursor: not-allowed; }
.module-empty { padding: 28px 8px 10px; color: var(--muted); text-align: center; }
.dialog-kicker { color: var(--brand-dark); font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
/* Confirmation dialog */
.confirm-dialog { width: min(420px, calc(100vw - 30px)); padding: 22px; border-top: 3px solid var(--danger); }
.confirm-dialog-icon { display: grid; width: 34px; height: 34px; place-items: center; margin-bottom: 12px; border-radius: 50%; background: #fff0d6; color: #a66b00; font-size: 20px; font-weight: 700; }
.confirm-dialog-content h2 { margin: 0; font-size: 18px; }
.confirm-dialog-content p { margin: 7px 0 0; color: var(--muted); overflow-wrap: anywhere; }
.confirm-dialog-actions { margin-top: 20px; padding-top: 14px; border-top: 1px solid var(--line); }
.confirm-dialog-actions .button { width: 82px; min-height: 30px; margin-top: 0; padding: 5px 8px; font-size: 12px; }
.toolbar-button, .toolbar-control { box-sizing: border-box; height: 30px; min-height: 30px; line-height: 18px; }
.toolbar-button { min-width: 58px; margin-top: 0; padding: 5px 8px; font-size: 12px; }
.toolbar-control { padding: 5px 7px; font-size: 14px; }
.admin-search-form { display: flex; flex: 0 1 auto; align-items: center; gap: 7px; margin: 0; }
.admin-search-form .toolbar-control { width: 180px; font-size: 14px; }
.admin-search-form input[type="search"]::-webkit-search-cancel-button { display: block; -webkit-appearance: searchfield-cancel-button; }
/* Desktop collapsed rail */
@media (min-width: 761px) {
    body.sidebar-collapsed .app-shell { grid-template-columns: 56px minmax(0, 1fr); }
    body.sidebar-collapsed .sidebar { padding-right: 10px; padding-left: 10px; }
    body.sidebar-collapsed .sidebar-header { justify-content: center; margin-bottom: 0; }
    body.sidebar-collapsed .brand,
    body.sidebar-collapsed .nav-list,
    body.sidebar-collapsed .logout-form { display: none; }
}

/* Mobile drawer */
@media (max-width: 760px) {
    .app-shell { display: block; }
    body.mobile-sidebar-open { overflow: hidden; }
    .mobile-menu-toggle { position: fixed; top: 12px; left: 12px; z-index: 1050; display: grid; width: 36px; height: 36px; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--text); box-shadow: 0 3px 12px rgba(20,35,50,.12); cursor: pointer; font-size: 19px; }
    .mobile-menu-toggle:hover { background: #eff4f8; }
    .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 1100; width: 280px; height: 100vh; padding: 20px 14px; overflow-y: auto; transform: translateX(-100%); transition: transform .2s ease; }
    body.mobile-sidebar-open .sidebar { transform: translateX(0); }
    .sidebar-header { margin-bottom: 24px; }
    .sidebar-toggle { display: none; }
    .mobile-sidebar-close { display: grid; }
    .mobile-nav-backdrop { position: fixed; inset: 0; z-index: 1090; display: block; background: rgba(17,29,40,.46); opacity: 0; visibility: hidden; transition: opacity .2s ease, visibility .2s ease; }
    body.mobile-sidebar-open .mobile-nav-backdrop { opacity: 1; visibility: visible; }
    .main-content { padding: 0 14px 24px; }
    input, select, textarea { font-size: 16px; }
    .topbar { min-height: 60px; padding-left: 48px; }
    .topbar { gap: 8px; }
    .topbar-actions { flex: 1 1 auto; justify-content: flex-end; }
    .current-chat-display { min-width: 0; justify-content: flex-end; }
    .current-chat-display .current-chat-label, .current-chat-display code { display: none; }
    .current-chat-display strong { max-width: min(46vw, 180px); font-size: 13px; }
    .toast-stack { right: 14px; bottom: 14px; width: calc(100vw - 28px); }
    .module-toolbar { align-items: flex-end; flex-wrap: wrap; }
    .admin-search-form { flex: 1 1 180px; }
    .admin-search-form .toolbar-control { width: 100%; font-size: 16px; }
    .confirm-dialog { width: calc(100vw - 28px); padding: 18px; }
}
