:root {
  --bg:          #ffffff;
  --surface:     #ffffff;
  --muted-bg:    #f1f5f9;
  --hover-bg:    #f6f8fb;
  --border:      #e2e8f0;
  --control:     #8492a6;
  --tab-active-border: #cbd5e1;
  --content:     1120px;
  --text:        #0f172a;
  --text-muted:  #475569;
  --text-subtle: #64748b;
  --primary:     #2563eb;
  --primary-hover: #1d4ed8;
  --focus:       #2563eb;
  --danger:      #b91c1c;
  --danger-solid:#dc2626;
  --danger-bg:   #fef2f2;
  --warning-text:#c2410c;
  --warning-bg:  #fffbeb;
  --success-text:#047857;
  --chart-neutral: #94a3b8;
  --chart-warning: #f97316;
  --chart-destructive: #ef4444;
  --radius:      8px;
  --radius-card: 12px;
  --radius-badge: 6px;
  --shadow-surface: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-raised: 0 16px 40px rgba(15, 23, 42, 0.16);
  --scrim:       rgba(15, 23, 42, 0.28);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
.appbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.appbar-inner { max-width: var(--content); margin: 0 auto; padding: 0 28px; height: 52px; display: flex; align-items: center; }
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-mark { width: 28px; height: 28px; border-radius: var(--radius); border: 1px solid var(--border); display: grid; place-items: center; color: var(--text); flex-shrink: 0; }
.brand-name { font-size: 14px; font-weight: 600; }
.wrap { max-width: var(--content); margin: 0 auto; padding: 28px 28px 60px; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.muted { color: var(--text-subtle); }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
input:focus-visible, select:focus-visible { outline-offset: 1px; }

.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.page-title { min-width: 0; }
.page-title h1 { margin: 0; font-size: 24px; line-height: 29px; font-weight: 600; letter-spacing: -0.02em; }
.page-title p { margin: 4px 0 0; font-size: 13px; line-height: 16px; color: var(--text-subtle); }
.page-actions { display: flex; gap: 8px; flex: none; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; height: 32px; padding: 0 12px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: 13px; font-weight: 500; cursor: pointer; transition: background .15s, border-color .15s; }
.btn:hover:not(:disabled) { background: var(--muted-bg); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger-solid); border-color: var(--danger-solid); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger); border-color: var(--danger); }
.btn-ghost-danger { background: transparent; border-color: transparent; color: var(--danger); }
.btn-ghost-danger:hover:not(:disabled) { background: var(--danger-bg); }
.btn-sm { padding: 0 10px; }
.btn-icon { width: 32px; min-height: 32px; height: 32px; padding: 0; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); margin-bottom: 24px; }
.kpi { padding: 10px 16px; border-left: 1px solid var(--border); min-width: 0; }
.kpi:first-child { border-left: 0; }
.kpi .l { font-size: 12px; color: var(--text-muted); }
.kpi .row-v { display: flex; align-items: baseline; gap: 8px; margin-top: 2px; flex-wrap: wrap; }
.kpi .v { font-size: 18px; font-weight: 600; color: var(--text); }
.kpi .s { font-size: 12px; color: var(--text-subtle); }
.kpi .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--chart-destructive); margin-right: 6px; vertical-align: 1px; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar-end { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.tabs { display: inline-flex; gap: 2px; padding: 2px; background: var(--muted-bg); border-radius: var(--radius); max-width: 100%; }
.tab { display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px; border: 1px solid transparent; border-radius: var(--radius-badge); background: transparent; color: var(--text-muted); font: inherit; font-size: 13px; font-weight: 500; white-space: nowrap; cursor: pointer; }
.tab:hover { color: var(--text); }
.tab .count { color: var(--text-muted); font-weight: 400; font-variant-numeric: tabular-nums; }
.tab[aria-pressed="true"] { background: var(--surface); border-color: var(--tab-active-border); color: var(--text); font-weight: 600; box-shadow: var(--shadow-surface); }
.search { position: relative; display: flex; align-items: center; color: var(--text-subtle); }
.search svg { position: absolute; left: 10px; pointer-events: none; }
#search { width: 260px; height: 32px; padding: 0 12px 0 32px; border: 1px solid var(--control); border-radius: var(--radius); font: inherit; font-size: 13px; background: var(--surface); color: var(--text); }
.result-count { font-size: 13px; color: var(--text-subtle); }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); overflow: hidden; }
.table-head, .row { display: grid; grid-template-columns: minmax(0, 2.2fr) minmax(0, 1fr) minmax(0, 2.2fr) 124px 128px; gap: 16px; align-items: center; padding: 0 16px; }
.table-head { min-height: 36px; background: var(--muted-bg); border-bottom: 1px solid var(--border); font-size: 13px; font-weight: 500; color: var(--text-muted); }
.row { min-height: 56px; padding-top: 8px; padding-bottom: 8px; border-bottom: 1px solid var(--border); font-size: 13px; }
.row:last-child { border-bottom: 0; }
.row:hover { background: var(--hover-bg); }
.m-name { font-weight: 500; font-size: 14px; overflow-wrap: anywhere; }
.m-sub { font-size: 12px; color: var(--text-subtle); margin-top: 2px; }
.m-loc { color: var(--text-muted); }

.bar-wrap { display: flex; flex-direction: column; gap: 6px; }
.bar-meta { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; }
.bar-meta .cur { font-weight: 600; font-size: 13px; font-variant-numeric: tabular-nums; }
.bar-track { position: relative; height: 6px; background: var(--muted-bg); border-radius: 3px; }
.bar-fill { height: 100%; border-radius: 3px; background: var(--chart-neutral); transition: width .25s; }
.bar-fill.low { background: var(--chart-destructive); }
.bar-fill.mid { background: var(--chart-warning); }
.bar-min { position: absolute; top: -3px; width: 2px; height: 12px; background: var(--text-muted); }

.book-cell { display: flex; flex-direction: column; gap: 4px; }
.book { display: flex; align-items: center; gap: 6px; }
.book input { width: 48px; height: 32px; padding: 0 6px; border: 1px solid var(--control); border-radius: var(--radius); font: inherit; font-size: 13px; text-align: center; color: var(--text); background: var(--surface); }
.book input[aria-invalid="true"] { border-color: var(--danger); }
.book-msg { margin: 0; font-size: 12px; color: var(--danger); max-width: 160px; }
.book-msg:empty { display: none; }

.badge { display: inline-flex; align-items: center; height: 20px; font-size: 12px; font-weight: 500; padding: 0 6px; border-radius: var(--radius-badge); vertical-align: 1px; }
.badge.reorder { background: var(--danger-bg); color: var(--danger); }
.row-actions { display: flex; justify-content: flex-end; }

.empty, .state-error { padding: 40px 24px; text-align: center; color: var(--text-subtle); }
.empty p, .state-error p { margin: 0 0 12px; }
.state-error .state-title { color: var(--text); font-weight: 600; font-size: 14px; margin-bottom: 4px; }
.loading { padding: 40px; text-align: center; color: var(--text-subtle); }
.spinner { width: 20px; height: 20px; border: 2px solid var(--border); border-top-color: var(--text-muted); border-radius: 50%; animation: spin .8s linear infinite; margin: 0 auto 10px; }
@keyframes spin { to { transform: rotate(360deg); } }

dialog.modal { padding: 0; border: 1px solid var(--border); border-radius: var(--radius-card); width: calc(100% - 32px); max-width: 460px; max-height: calc(100dvh - 32px); background: var(--surface); color: var(--text); box-shadow: var(--shadow-raised); }
dialog.modal::backdrop { background: var(--scrim); }
.modal-inner { animation: pop .14s ease; }
@keyframes pop { from { transform: translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { padding: 20px 20px 4px; }
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.modal-head p { margin: 4px 0 0; font-size: 13px; color: var(--text-subtle); }
.modal-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { padding: 12px 20px 20px; display: flex; gap: 8px; justify-content: flex-end; align-items: center; flex-wrap: wrap; }
.modal-foot .spacer { flex: 1; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field label { font-size: 13px; font-weight: 500; color: var(--text); }
.field .req { color: var(--danger); }
.field input, .field select { width: 100%; min-height: 36px; padding: 0 12px; border: 1px solid var(--control); border-radius: var(--radius); font: inherit; font-size: 14px; background: var(--surface); color: var(--text); }
.field [aria-invalid="true"] { border-color: var(--danger); }
.hint { margin: 0; font-size: 12px; color: var(--text-subtle); }
.field-error { margin: 0; font-size: 12px; font-weight: 500; color: var(--danger); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.history h3 { margin: 4px 0 8px; font-size: 13px; font-weight: 600; }
.history ul { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--border); }
.history li { display: grid; grid-template-columns: 1fr auto auto; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.history time { color: var(--text-subtle); font-variant-numeric: tabular-nums; }
.history .pos { color: var(--success-text); font-weight: 500; }
.history .neg { color: var(--danger); font-weight: 500; }

#toast-root { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; width: min(560px, calc(100% - 32px)); }
.toast { display: flex; align-items: center; gap: 12px; background: var(--text); color: #f8fafc; padding: 10px 12px 10px 16px; border-radius: var(--radius); font-size: 13px; box-shadow: var(--shadow-raised); animation: pop .2s ease; transition: opacity .2s; }
.toast span { flex: 1; }
.toast.err span::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--chart-destructive); margin-right: 8px; vertical-align: 1px; }
.toast.leaving { opacity: 0; }
.toast-action { flex: none; min-height: 32px; padding: 0 12px; border-radius: 6px; border: 1px solid #94a3b8; background: transparent; color: #fff; font: inherit; font-weight: 600; font-size: 13px; cursor: pointer; }
.toast-action:focus-visible { outline-color: #fff; }

@media (max-width: 720px) {
  .wrap, .appbar-inner { padding-left: 16px; padding-right: 16px; }
  .wrap { padding-top: 20px; padding-bottom: 120px; }
}
@media (max-width: 820px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi:nth-child(3) { border-left: 0; }
  .kpi:nth-child(n+3) { border-top: 1px solid var(--border); }
  .table-head { display: none; }
  .row { grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "name edit" "loc loc" "bar bar" "book book"; gap: 10px; padding: 14px 16px; }
  .cell-name { grid-area: name; }
  .row-actions { grid-area: edit; align-self: start; }
  .m-loc { grid-area: loc; }
  .bar-wrap { grid-area: bar; }
  .book-cell { grid-area: book; }
  .btn-icon, .book input { width: 44px; height: 44px; min-height: 44px; }
  .book input { width: 72px; }
  .row-actions .btn { height: 44px; }
  #search { height: 40px; }
  .toolbar, .toolbar-end { width: 100%; }
  .search { flex: 1 1 100%; }
  #search { width: 100%; }
  .tabs { overflow-x: auto; flex-wrap: nowrap; }
  .book-msg { max-width: none; }
}
@media (max-width: 520px) {
  .field-row { grid-template-columns: 1fr; }
  .row-actions .btn span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .row-actions .btn { width: 44px; padding: 0; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
