:root {
    --bg: #f4f4f6;
    --surface: #ffffff;
    --surface-2: #f8f8fa;
    --surface-3: #eeeef2;
    --text: #18181b;
    --text-2: #3f3f46;
    --muted: #71717a;
    --border: #e4e4e7;
    --border-strong: #d4d4d8;
    --primary: #18181b;
    --primary-hover: #27272a;
    --on-primary: #ffffff;
    --accent: #4f46e5;
    --accent-soft: #eef2ff;
    --success: #15803d;
    --success-soft: #dcfce7;
    --warning: #b45309;
    --warning-soft: #fef3c7;
    --danger: #dc2626;
    --danger-soft: #fee2e2;
    --info: #1d4ed8;
    --info-soft: #dbeafe;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 2px rgba(24, 24, 27, .06), 0 1px 3px rgba(24, 24, 27, .08);
    --shadow-lg: 0 12px 32px rgba(24, 24, 27, .18);
    --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --sidebar-w: 236px;
    --topbar-h: 60px;
    --bottom-nav-h: 64px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.45;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; line-height: 1.25; font-weight: 650; }
h1 { font-size: 20px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0 0 8px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; display: block; }
svg.icon { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
svg.icon-sm { width: 16px; height: 16px; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.right { text-align: right; }
.center { text-align: center; }
.strong { font-weight: 650; }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.mt-0 { margin-top: 0; }
.mt { margin-top: 16px; }
.mb { margin-bottom: 16px; }
.gap-sm { gap: 8px; }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.grow { flex: 1 1 auto; min-width: 0; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 42px;
    padding: 0 16px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-weight: 560;
    cursor: pointer;
    white-space: nowrap;
    text-decoration: none;
    transition: background .15s, border-color .15s, color .15s;
    -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:focus-visible, .input:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-accent:hover { background: #4338ca; }
.btn-success { background: var(--success); border-color: var(--success); color: #fff; }
.btn-success:hover { background: #166534; }
.btn-danger { color: var(--danger); border-color: #fecaca; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-3); }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn-lg { min-height: 52px; padding: 0 22px; font-size: 16px; }
.btn-block { width: 100%; }
.btn-icon { width: 42px; padding: 0; }
.btn-icon.btn-sm { width: 34px; }
.btn.loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.loading::after {
    content: "";
    position: absolute;
    width: 18px; height: 18px;
    border: 2px solid currentColor;
    border-color: var(--muted) transparent var(--muted) transparent;
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    min-width: 0;
}
.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}
.card-body { padding: 16px; }
.card-body.flush { padding: 0; }
.card-footer { padding: 12px 16px; border-top: 1px solid var(--border); }

.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 900px) {
    .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
}

.kpis { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
    box-shadow: var(--shadow);
    min-width: 0;
}
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-value { font-size: 22px; font-weight: 650; margin-top: 4px; }
.kpi-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }
.kpi.hero .kpi-value { font-size: 26px; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .label { font-size: 13px; font-weight: 560; color: var(--text-2); }
.field .hint { font-size: 12px; color: var(--muted); }
.field .error-text { font-size: 12px; color: var(--danger); }
.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.form-grid .span-all { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-top: 16px; }

.input, .select, .textarea {
    width: 100%;
    min-height: 42px;
    padding: 9px 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 16px;
}
.textarea { min-height: 84px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input.invalid, .select.invalid, .textarea.invalid { border-color: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
.input-sm { min-height: 36px; padding: 6px 10px; font-size: 15px; }
.input-group { position: relative; display: flex; align-items: center; }
.input-group .input { padding-left: 40px; }
.input-group > .icon { position: absolute; left: 12px; color: var(--muted); pointer-events: none; }
.checkbox { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; user-select: none; min-height: 34px; }
.checkbox input { width: 18px; height: 18px; accent-color: var(--primary); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 36px;
    padding: 0 12px;
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    background: var(--surface);
    font: inherit;
    font-size: 14px;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}
.chip:hover { background: var(--surface-3); }
.chip.active { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.chip:disabled { opacity: .45; cursor: not-allowed; text-decoration: line-through; }
.swatch { width: 14px; height: 14px; border-radius: 50%; border: 1px solid rgba(0, 0, 0, .2); flex: none; display: inline-block; }

.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    background: var(--surface-3);
    color: var(--text-2);
    white-space: nowrap;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-dark { background: var(--primary); color: var(--on-primary); }

.table-wrap { width: 100%; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .03em;
    padding: 10px 12px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td { background: var(--surface-2); }
.table tr.muted-row td { color: var(--muted); }
.table .right, .table th.right { text-align: right; }
.table td.actions { text-align: right; white-space: nowrap; }
.table tfoot td { font-weight: 650; background: var(--surface-2); }

@media (max-width: 720px) {
    .table-cards thead { display: none; }
    .table-cards, .table-cards tbody, .table-cards tr, .table-cards td { display: block; width: 100%; }
    .table-cards tr { padding: 10px 14px; border-bottom: 1px solid var(--border); }
    .table-cards tbody tr:last-child { border-bottom: none; }
    .table-cards td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 4px 0; border: none; text-align: right; }
    .table-cards td::before { content: attr(data-label); color: var(--muted); font-size: 13px; text-align: left; flex: none; }
    .table-cards td[data-label=""]::before, .table-cards td:not([data-label])::before { content: none; }
    .table-cards td.primary-cell { font-weight: 650; justify-content: flex-start; text-align: left; font-size: 15px; }
    .table-cards td.actions { justify-content: flex-end; padding-top: 8px; }
    .table-cards tfoot tr { background: var(--surface-2); }
}

.bar-cell { display: flex; align-items: center; gap: 8px; min-width: 120px; }
.bar-track { flex: 1; height: 8px; background: var(--surface-3); border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 0 4px 4px 0; }

.empty { padding: 32px 16px; text-align: center; color: var(--muted); }
.empty .icon { width: 36px; height: 36px; margin: 0 auto 8px; display: block; opacity: .6; }

.pagination { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.pagination .pages { display: flex; gap: 6px; align-items: center; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; margin-bottom: 16px; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
    padding: 10px 14px;
    border: none;
    background: none;
    font: inherit;
    font-weight: 560;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    cursor: pointer;
    white-space: nowrap;
}
.tab.active { color: var(--text); border-bottom-color: var(--primary); }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 16px; }
.toolbar .field { min-width: 150px; flex: 1 1 150px; }
.toolbar .field.wide { flex: 3 1 240px; }
.toolbar .field.narrow { flex: 0 1 150px; }

dialog.modal {
    width: min(560px, calc(100vw - 24px));
    max-height: calc(100dvh - 24px);
    padding: 0;
    border: none;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    background: var(--surface);
    color: var(--text);
    overflow: hidden;
}
dialog.modal.wide { width: min(860px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(24, 24, 27, .5); }
dialog.modal[open] { display: flex; flex-direction: column; }
.modal-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 16px; overflow-y: auto; flex: 1 1 auto; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; background: var(--surface-2); }
@media (max-width: 600px) {
    dialog.modal, dialog.modal.wide {
        width: 100vw;
        max-width: 100vw;
        max-height: 92dvh;
        margin: auto 0 0;
        border-radius: var(--radius) var(--radius) 0 0;
    }
    .modal-footer { padding-bottom: calc(12px + var(--safe-bottom)); }
    .modal-footer .btn { flex: 1 1 auto; }
}

.toasts { position: fixed; top: 16px; right: 16px; z-index: 1000; display: flex; flex-direction: column; gap: 8px; max-width: min(380px, calc(100vw - 32px)); }
.toast {
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: #fff;
    box-shadow: var(--shadow-lg);
    font-size: 14px;
    animation: toast-in .2s ease-out;
}
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }
.toast.success { background: var(--success); }
@keyframes toast-in { from { transform: translateY(-8px); opacity: 0; } }

.skeleton { background: linear-gradient(90deg, var(--surface-3), var(--surface-2), var(--surface-3)); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 6px; min-height: 16px; }
@keyframes shimmer { to { background-position: -200% 0; } }

.divider { height: 1px; background: var(--border); margin: 12px 0; }
.list { list-style: none; margin: 0; padding: 0; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
a.list-item { color: inherit; }
a.list-item:hover { background: var(--surface-2); text-decoration: none; }
.thumb { width: 44px; height: 44px; border-radius: var(--radius-sm); object-fit: cover; background: var(--surface-3); flex: none; display: flex; align-items: center; justify-content: center; color: var(--muted); overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .icon { width: 20px; height: 20px; }
.dl { display: grid; grid-template-columns: minmax(110px, auto) 1fr; gap: 8px 16px; margin: 0; font-size: 14px; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; overflow-wrap: anywhere; }

.field-row { display: flex; gap: 8px; align-items: stretch; }
.field-row > .select, .field-row > .input { flex: 1 1 auto; min-width: 0; width: auto; }
.field-row > .btn { flex: none; }

@media (max-width: 600px) {
    .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
    .kpi { padding: 12px; }
    .kpi-value { font-size: 17px; }
    .kpi.hero { grid-column: 1 / -1; }
    .kpi.hero .kpi-value { font-size: 24px; }
}
