:root {
  --brand: #e8731a;
  --brand-ink: #ffffff;
  --brand-soft: #fdebdc;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --ink: #1d2433;
  --ink-2: #5b6474;
  --line: #e2e5ea;
  --ok: #1f9d55;
  --ok-soft: #ddf3e6;
  --warn: #c98a00;
  --warn-soft: #fff4d1;
  --bad: #d23c3c;
  --bad-soft: #fde3e3;
  --info: #2f6fd6;
  --info-soft: #e1ecfd;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  --side: 232px;
  --top: 56px;
  --cell: 22px; /* planner half-day width */
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1f27;
    --surface-2: #20262f;
    --ink: #e7eaf0;
    --ink-2: #9aa3b2;
    --line: #2c333e;
    --brand-soft: #3a2614;
    --ok-soft: #12321f;
    --warn-soft: #3a2f0d;
    --bad-soft: #3d1717;
    --info-soft: #14284a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--brand); text-decoration: none; }
h1, h2, h3 { margin: 0 0 .5em; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--ink-2); }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.grow { flex: 1; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: var(--side) 1fr; min-height: 100vh; }
.side {
  background: #1d2433; color: #cfd5df; position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 16px; color: #fff; font-weight: 700; }
.brand img { width: 34px; height: 34px; border-radius: 8px; }
.brand small { display: block; font-weight: 400; color: #9aa3b2; font-size: 11px; }
.nav { padding: 6px 8px 16px; display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; color: #cfd5df; font-weight: 500;
}
.nav a svg { width: 18px; height: 18px; flex: none; opacity: .85; }
.nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav a.active { background: var(--brand); color: #fff; }
.nav .sep { height: 1px; background: rgba(255, 255, 255, .08); margin: 8px 6px; }
.side-foot { margin-top: auto; padding: 12px 16px; font-size: 12px; color: #9aa3b2; border-top: 1px solid rgba(255,255,255,.08); }
.side-foot button { margin-top: 6px; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: var(--top); display: flex; align-items: center; gap: 10px; padding: 0 20px; background: var(--surface);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.topbar h1 { margin: 0; font-size: 1.1rem; }
.menu-btn { display: none; }
.content { padding: 20px; flex: 1; min-width: 0; }
.offline-badge { background: var(--warn-soft); color: var(--warn); border-radius: 99px; padding: 2px 10px; font-size: 12px; font-weight: 600; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 50; left: 0; top: 0; width: 270px; transform: translateX(-100%); transition: transform .2s; }
  .side.open { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .45); }
  .menu-btn { display: inline-flex; }
  .content { padding: 14px; }
  .topbar { padding: 0 12px; }
}

/* ---------- components ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card { margin-top: 16px; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 1100px) { .cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 640px) { .cols-2, .cols-3, .cols-4 { grid-template-columns: 1fr; } }

.stat { display: flex; flex-direction: column; gap: 4px; }
.stat .v { font-size: 1.9rem; font-weight: 700; }
.stat .l { color: var(--ink-2); font-size: 13px; }
a.stat:hover { border-color: var(--brand); }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; margin-bottom: 14px; }
.toolbar .field { margin: 0; }

button, .btn {
  font: inherit; cursor: pointer; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 8px 14px; border-radius: 8px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  min-height: 38px;
}
button:hover, .btn:hover { border-color: var(--ink-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.primary:hover { filter: brightness(1.05); border-color: var(--brand); }
.danger { color: var(--bad); }
.danger:hover { border-color: var(--bad); }
.ghost { background: transparent; border-color: transparent; }
.icon-btn { padding: 6px; min-height: 0; background: transparent; border-color: transparent; color: inherit; }
.icon-btn svg { width: 22px; height: 22px; }
.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; font-weight: 600; color: var(--ink-2); }
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 10px; min-height: 38px; width: 100%; min-width: 0;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
input[readonly] { background: var(--surface-2); color: var(--ink-2); }
input[type=checkbox] { width: 18px; height: 18px; min-height: 0; accent-color: var(--brand); }
.check { flex-direction: row; align-items: center; gap: 8px; padding-top: 18px; }
.check > span { font-size: 13px; color: var(--ink); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.form-grid .span-2 { grid-column: span 2; }
.form-grid .span-all { grid-column: 1 / -1; }
@media (max-width: 480px) { .form-grid { grid-template-columns: 1fr 1fr; } .form-grid .span-2 { grid-column: 1 / -1; } }
.row { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.row > .field { flex: 1; }
.inline-time { display: grid; grid-template-columns: 1fr 110px; gap: 6px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.sticky-actions { position: sticky; bottom: 0; background: var(--surface); padding: 12px 16px; margin: 16px -16px -16px; border-top: 1px solid var(--line); border-radius: 0 0 var(--radius) var(--radius); z-index: 5; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; color: var(--ink-2); padding: 8px 12px; }
.tabs button.active { color: var(--brand); border-bottom-color: var(--brand); }

/* tables */
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { background: var(--surface-2); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--ink-2); position: sticky; top: 0; z-index: 1; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--brand-soft); }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
tfoot td { font-weight: 700; background: var(--surface-2); }
.empty { padding: 24px; text-align: center; color: var(--ink-2); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.info { background: var(--info-soft); color: var(--info); }

/* money summary */
.totals { display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; font-variant-numeric: tabular-nums; }
.totals .t { color: var(--ink-2); }
.totals .v { text-align: right; font-weight: 600; }
.totals .big { font-size: 1.15rem; font-weight: 800; color: var(--ink); }
.totals .due { color: var(--bad); }

/* price lines */
.price-table td, .price-table th { padding: 6px 6px; border: 0; }
.price-table input { min-height: 34px; padding: 5px 8px; text-align: right; }
.price-table td:first-child { font-weight: 600; white-space: nowrap; color: var(--ink-2); font-size: 13px; padding-top: 12px; }
.price-table .out { text-align: right; font-weight: 700; padding-top: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, #1d2433 0%, #2b3446 55%, var(--brand) 160%); }
.login { width: 100%; max-width: 380px; }
.login .brand { color: var(--ink); padding: 0 0 12px; font-size: 1.15rem; }
.login .brand img { width: 46px; height: 46px; }
.login form { display: grid; gap: 12px; }
.err { color: var(--bad); font-weight: 600; min-height: 1.2em; }

/* dialog / toast */
dialog { border: 0; border-radius: 12px; padding: 0; background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .35); width: min(720px, calc(100vw - 24px)); max-height: calc(100vh - 32px); }
dialog::backdrop { background: rgba(10, 14, 22, .55); }
dialog .dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
dialog .dlg-head h2 { margin: 0; }
dialog .dlg-body { padding: 16px; overflow: auto; max-height: calc(100vh - 160px); }
dialog .dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
dialog.narrow { width: min(420px, calc(100vw - 24px)); }
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 1000; }
.toast { background: #1d2433; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; animation: pop .15s ease-out; }
.toast.bad { background: var(--bad); }
.toast.ok { background: var(--ok); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

/* search popup */
.ac { position: relative; }
.ac-list { position: absolute; left: 0; right: 0; top: 100%; z-index: 30; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); max-height: 280px; overflow: auto; margin-top: 2px; }
.ac-list div { padding: 8px 10px; cursor: pointer; border-bottom: 1px solid var(--line); }
.ac-list div:hover, .ac-list div.sel { background: var(--brand-soft); }
.ac-list small { color: var(--ink-2); }

/* ---------- planner ---------- */
.planner { overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); max-height: calc(100vh - 190px); position: relative; }
.pl-grid { position: relative; }
.pl-head { position: sticky; top: 0; z-index: 4; display: flex; background: var(--surface-2); border-bottom: 1px solid var(--line); }
.pl-corner { position: sticky; left: 0; z-index: 5; background: var(--surface-2); flex: none; width: 190px; border-right: 1px solid var(--line); padding: 6px 8px; font-size: 12px; color: var(--ink-2); display: flex; align-items: end; }
.pl-days { display: flex; }
.pl-day { width: calc(var(--cell) * 2); flex: none; text-align: center; font-size: 11px; border-right: 1px solid var(--line); padding: 3px 0; line-height: 1.2; }
.pl-day b { display: block; font-size: 13px; color: var(--ink); }
.pl-day.we { background: rgba(232, 115, 26, .07); }
.pl-day.today { background: var(--info); color: #fff; }
.pl-day.today b { color: #fff; }
.pl-day.m1 { border-left: 2px solid var(--ink-2); }
.pl-day .mon { display: block; font-weight: 700; color: var(--brand); font-size: 10px; text-transform: uppercase; }
.pl-row { display: flex; border-bottom: 1px solid var(--line); height: 42px; }
.pl-car { position: sticky; left: 0; z-index: 3; background: var(--surface); width: 190px; flex: none; padding: 4px 8px; border-right: 1px solid var(--line); font-size: 12px; line-height: 1.25; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; cursor: default; }
.pl-car b { display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.pl-track { position: relative; flex: none; cursor: copy;
  background-image: repeating-linear-gradient(to right, transparent 0, transparent calc(var(--cell) * 2 - 1px), var(--line) calc(var(--cell) * 2 - 1px), var(--line) calc(var(--cell) * 2)); }
.pl-today { position: absolute; top: 0; bottom: 0; background: rgba(47, 111, 214, .10); pointer-events: none; }
.bar {
  position: absolute; top: 4px; bottom: 4px; border-radius: 6px; padding: 2px 6px; font-size: 11px; line-height: 1.3; overflow: hidden;
  white-space: nowrap; text-overflow: ellipsis; cursor: pointer; color: #1d2433; border: 1px solid rgba(0, 0, 0, .15); box-shadow: 0 1px 1px rgba(0,0,0,.08);
}
.bar b { font-size: 12px; }
.bar.c0 { background: #cfe0f6; }
.bar.c1 { background: #fde68a; }
.bar.out { background: #86efac; }       /* goes out today */
.bar.in { background: #fca5a5; }        /* comes back today */
.bar.ext { background: #fde047; outline: 2px dashed #a16207; outline-offset: -2px; }
.bar.cx { background: #d1d5db; text-decoration: line-through; }
.bar:hover { filter: brightness(.95); z-index: 2; overflow: visible; min-width: max-content; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin-top: 8px; }
.legend i { display: inline-block; width: 14px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; border: 1px solid rgba(0,0,0,.15); }

/* daily sheet */
.daily h2 { display: flex; align-items: center; gap: 8px; }
.daily .count { background: var(--brand); color: #fff; border-radius: 99px; padding: 0 9px; font-size: 13px; }

/* print voucher */
#print-area { display: none; }
@media print {
  #root, .toasts, dialog { display: none !important; }

  #print-area { display: block; padding: 0; color: #000; background: #fff; font-size: 12px; }
  #print-area table { width: 100%; border-collapse: collapse; }
  #print-area td, #print-area th { border: 1px solid #999; padding: 4px 6px; }
  #print-area h1 { font-size: 18px; }
  .print-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  @page { margin: 8mm; }
}
