/* ---------- Tokens ---------- */

/* Each colour is light-dark(light, dark). The theme toggle sets data-theme, which only changes color-scheme. */
:root {
  color-scheme: light dark;
  --bg: light-dark(#f6f7f9, #111318);
  --surface: light-dark(#ffffff, #1a1d24);
  --surface-2: light-dark(#f0f2f5, #22262f);
  --border: light-dark(#e1e5ea, #2e333d);
  --text: light-dark(#16191f, #e8eaee);
  --text-2: light-dark(#5b6573, #a3abb8);
  --text-3: light-dark(#8b94a1, #747d8b);
  --brand: light-dark(#1f3a5f, #9cc0ea);
  --accent: light-dark(#2563eb, #6ea0ff);
  --accent-soft: light-dark(#e8efff, #1d2a45);
  --ok: light-dark(#15803d, #4ade80);
  --ok-soft: light-dark(#e7f6ec, #12291b);
  --warn: light-dark(#b45309, #fbbf24);
  --warn-soft: light-dark(#fdf3e2, #2e2410);
  --danger: light-dark(#b91c1c, #f87171);
  --danger-soft: light-dark(#fdecec, #331717);
  --shadow: 0 1px 2px light-dark(rgb(16 24 40 / 0.06), transparent), 0 1px 3px light-dark(rgb(16 24 40 / 0.08), transparent);
  --radius: 10px;
  --radius-sm: 6px;
  --nav-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

/* ---------- Base ---------- */

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

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { margin: 0; line-height: 1.25; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0 0 12px; }

img { max-width: 100%; display: block; }

.muted { color: var(--text-2); }
.small { font-size: 13px; }
.money, .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.right { text-align: right; }
.hidden { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Layout ---------- */

.shell { min-height: 100vh; }

.sidebar {
  display: none;
  position: fixed;
  inset: 0 auto 0 0;
  width: 232px;
  padding: 20px 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  flex-direction: column;
  gap: 2px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px 18px;
  font-weight: 700;
  font-size: 17px;
  color: var(--text);
}
.brand img { width: 28px; height: 28px; }

.nav-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav-link.active { background: var(--accent-soft); color: var(--accent); }
.nav-heading { padding: 14px 10px 4px; font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-3); }
.nav-sep { height: 1px; background: var(--border); margin: 10px 4px; }
.sidebar .grow { flex: 1; }
.theme-toggle { justify-content: flex-start; margin: 0 4px; color: var(--text-2); }

.main {
  padding: 16px 16px calc(var(--nav-h) + 24px + env(safe-area-inset-bottom));
  max-width: 1080px;
  margin: 0 auto;
}

.bottombar {
  position: fixed;
  inset: auto 0 0 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  align-items: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 20;
}
.bottombar a, .bottombar button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: var(--text-2);
  background: none;
  border: 0;
  font-family: inherit;
}
.bottombar a.active { color: var(--accent); }
.bottombar a:hover { text-decoration: none; }
.bottombar .icon { width: 22px; height: 22px; }
.bottombar .add {
  width: 52px;
  height: 52px;
  margin: 0 auto;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  justify-content: center;
  box-shadow: var(--shadow);
}

@media (min-width: 900px) {
  .sidebar { display: flex; }
  .bottombar { display: none; }
  .main { margin-left: 232px; padding: 28px 36px 48px; }
}

/* ---------- Page structure ---------- */

.page { display: flex; flex-direction: column; gap: 16px; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.back { font-size: 14px; color: var(--text-2); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card > h2 { margin-bottom: 12px; }
.card.flush { padding: 0; overflow: hidden; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { padding: 14px 16px; }
.stat .label { font-size: 13px; color: var(--text-2); }
.stat .value { font-size: 22px; font-weight: 600; margin-top: 2px; }

.split { display: grid; gap: 16px; }
@media (min-width: 900px) {
  .split { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
}

/* ---------- Lists ---------- */

.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-top: 1px solid var(--border); }

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: inherit;
}
a.row:hover { background: var(--surface-2); text-decoration: none; }
.row .body { flex: 1; min-width: 0; }
.row .title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { font-size: 13px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .side { text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }

.thumb {
  width: 44px;
  height: 44px;
  flex: none;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: hidden;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-3);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }

.empty { padding: 32px 16px; text-align: center; color: var(--text-2); }
.empty .actions { margin-top: 12px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.list-foot { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; justify-content: center; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table th { font-size: 12px; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.03em; }
.table tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }

/* ---------- Chips ---------- */

.chip {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text-2);
  white-space: nowrap;
}
button.chip { border: 0; cursor: pointer; font-family: inherit; }
button.chip:hover { background: var(--accent-soft); color: var(--accent); }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }
.chip.info { background: var(--accent-soft); color: var(--accent); }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: 500 14px var(--font);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { min-height: 32px; padding: 0 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn-row .spacer { flex: 1; }

/* ---------- Forms ---------- */

.form { display: flex; flex-direction: column; gap: 14px; }
.form-grid { display: grid; gap: 14px; grid-template-columns: 1fr 1fr; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 520px) { .form-grid { grid-template-columns: 1fr; } }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 500; color: var(--text-2); }
.field .hint { font-size: 12px; color: var(--text-3); }

input, select, textarea {
  width: 100%;
  min-height: 42px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: 16px var(--font); /* 16px stops iOS zooming on focus */
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[aria-invalid="true"], select[aria-invalid="true"] { border-color: var(--danger); }
input[type="checkbox"] { width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--accent); }

.check { display: flex; align-items: center; gap: 10px; font-size: 14px; cursor: pointer; }

.form-error {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 14px;
}
.notice {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--warn-soft);
  color: var(--warn);
  font-size: 14px;
}

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.toolbar > * { flex: 1 1 140px; }
.toolbar .search { flex: 2 1 220px; }

/* ---------- Auth screens ---------- */

.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.auth .card { width: 100%; max-width: 400px; padding: 28px 24px; }
.auth .brand { justify-content: center; padding-bottom: 8px; font-size: 20px; }
.auth h1 { font-size: 18px; text-align: center; margin-bottom: 4px; }
.auth .lead { text-align: center; color: var(--text-2); font-size: 14px; margin-bottom: 20px; }

.secret {
  font-family: var(--mono);
  font-size: 15px;
  letter-spacing: 0.06em;
  word-break: break-all;
  padding: 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  text-align: center;
  user-select: all;
}
.codes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 16px; font-family: var(--mono); padding: 12px; background: var(--surface-2); border-radius: var(--radius-sm); }


.divider { display: flex; align-items: center; gap: 12px; margin: 18px 0; font-size: 13px; color: var(--text-3); }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

/* ---------- Uploads ---------- */

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 24px 16px;
  text-align: center;
  color: var(--text-2);
  background: var(--surface);
  transition: border-color 0.15s, background 0.15s;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .btn-row { justify-content: center; margin-top: 12px; }

.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width 0.2s; }

/* ---------- Receipt viewer ---------- */

.viewer {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 280px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.viewer .stage { flex: 1; display: grid; place-items: center; min-height: 280px; }
.viewer .stage img { max-height: 70vh; width: auto; margin: auto; }
.viewer .stage iframe { width: 100%; height: 70vh; border: 0; background: #fff; }
.viewer .strip { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--border); overflow-x: auto; align-items: center; }
.viewer .strip .thumb { cursor: pointer; }
.viewer .strip .thumb.active { outline: 2px solid var(--accent); }
.viewer .strip .btn-row { margin-left: auto; }
@media (min-width: 900px) { .sticky { position: sticky; top: 24px; } }

.kbd {
  display: inline-block;
  padding: 0 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  font: 12px var(--mono);
  color: var(--text-2);
}

/* ---------- Tabs ---------- */

.tabs { display: flex; gap: 4px; overflow-x: auto; border-bottom: 1px solid var(--border); }
.tabs a {
  padding: 8px 12px;
  color: var(--text-2);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  font-weight: 500;
}
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- Overlays ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(440px, calc(100vw - 32px));
  box-shadow: 0 20px 50px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(15 20 30 / 0.45); }
dialog h2 { margin-bottom: 8px; }
dialog .btn-row { justify-content: flex-end; margin-top: 18px; }

dialog.sheet { margin: auto auto 0; width: 100%; max-width: 520px; border-radius: 16px 16px 0 0; padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
@media (min-width: 900px) { dialog.sheet { margin: auto; border-radius: var(--radius); } }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; }
dialog.sheet p { margin: 14px 0 8px; }
.sheet-actions .btn { justify-content: flex-start; min-height: 52px; font-size: 15px; }

.toasts {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  width: min(420px, calc(100vw - 32px));
}
@media (min-width: 900px) { .toasts { bottom: 24px; left: auto; right: 24px; transform: none; } }
.toast {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
}
.toast.error { background: var(--danger); color: #fff; }
.toast.warn { background: var(--warn); color: #fff; }
.toast a { color: inherit; text-decoration: underline; }

.loading { padding: 48px 0; text-align: center; color: var(--text-3); }

/* ---------- Invoice paper ---------- */

.paper {
  position: relative;
  background: #fff;
  color: #16191f;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 40px 44px;
  font-size: 13px;
  line-height: 1.5;
  overflow: hidden;
}
.paper table { border-collapse: collapse; }
.paper-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin-bottom: 28px; }
.paper-title { font-size: 30px; font-weight: 300; letter-spacing: 0.1em; text-transform: uppercase; }
.paper-meta { min-width: 240px; }
.paper-parties { display: grid; grid-template-columns: 1fr 1fr; gap: 32px; margin-bottom: 28px; }
.paper-label { font-size: 11px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: #16191f; border-bottom: 2px solid #16191f; padding-bottom: 4px; margin-bottom: 8px; }
.paper-sub { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: #8b94a1; margin: 10px 0 2px; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 8px; padding: 1px 0; }
.kv .k { color: #5b6573; }
.paper-meta .kv { grid-template-columns: 1fr auto; text-align: right; }
.paper-meta .kv .k { text-align: left; }
.paper-items { width: 100%; margin-bottom: 16px; }
.paper-items th { text-align: left; font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: #5b6573; border-bottom: 2px solid #16191f; padding: 6px 8px; }
.paper-items td { padding: 8px; border-bottom: 1px solid #e1e5ea; vertical-align: top; }
.paper-items .right, .paper-totals td { text-align: right; }
.paper-totals { margin-left: auto; min-width: 260px; }
.paper-totals th { text-align: left; font-weight: 500; color: #5b6573; padding: 4px 16px 4px 0; }
.paper-totals td { padding: 4px 0; }
.paper-totals .grand th, .paper-totals .grand td { font-size: 16px; font-weight: 700; color: #16191f; border-top: 2px solid #16191f; padding-top: 8px; }
.paper-notes { margin-top: 24px; }
.watermark {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-24deg);
  font-size: 110px;
  font-weight: 800;
  color: rgb(185 28 28 / 0.08);
  pointer-events: none;
}
@media (max-width: 600px) {
  .paper { padding: 24px 18px; }
  .paper-head { flex-direction: column; }
  .paper-parties { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 110px 1fr; }
}

/* ---------- Invoice page ---------- */

.invoice-layout { display: grid; gap: 16px; align-items: start; }
.invoice-layout .paper { max-width: 860px; }
@media (min-width: 1400px) { .invoice-layout { grid-template-columns: minmax(0, 860px) minmax(260px, 1fr); } }

.parties { display: grid; gap: 16px; grid-template-columns: 1fr 1fr; }
@media (max-width: 760px) { .parties { grid-template-columns: 1fr; } }
fieldset { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px; margin: 0; min-width: 0; }
legend { font-weight: 600; padding: 0 6px; }

/* ---------- Time ---------- */

.strong { font-weight: 600; }
.timer-row { display: flex; align-items: center; gap: 16px; margin-bottom: 12px; }
.timer-row .body { flex: 1; min-width: 0; }
.timer-clock { font-size: 28px; font-weight: 600; }
.week-nav { align-items: center; }
.week-nav > * { flex: 0 0 auto; }
.week-nav select { flex: 0 1 200px; margin-left: auto; }
.timesheet th, .timesheet td { white-space: nowrap; }
.timesheet th:first-child { min-width: 180px; white-space: normal; }
.timesheet .today { color: var(--accent); }
.timesheet tfoot th, .timesheet tfoot td { border-top: 2px solid var(--border); font-weight: 600; }
.hours-input { width: 64px; min-height: 34px; padding: 4px 6px; text-align: right; font-size: 15px; }

.timer-chip {
  position: fixed;
  z-index: 30;
  right: 16px;
  bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 6px 6px 14px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.2);
  font-size: 14px;
}
.timer-chip a { color: inherit; text-decoration: none; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.timer-chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); animation: pulse 1.6s infinite; }
.timer-chip .btn { min-height: 30px; border-radius: 999px; }
@keyframes pulse { 50% { opacity: 0.3; } }
@media (min-width: 900px) { .timer-chip { top: 16px; bottom: auto; right: 24px; } }
@media print { .timer-chip { display: none; } }

a.chip:hover { text-decoration: none; filter: brightness(0.95); }
.stat.clickable { cursor: pointer; }
.stat.clickable:hover { border-color: var(--accent); }
.stat.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.row-button { all: unset; cursor: pointer; flex: 1; min-width: 0; }
.row-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Line item editor ---------- */

.lines { display: flex; flex-direction: column; gap: 8px; }
.line { display: grid; grid-template-columns: minmax(220px, 1fr) 130px 90px 130px 110px 36px; gap: 8px; align-items: start; }
.line .amount { padding-top: 10px; text-align: right; }
.line-head { font-size: 12px; font-weight: 600; color: var(--text-2); }
@media (max-width: 760px) {
  .line { grid-template-columns: 1fr 1fr; }
  .line > :first-child { grid-column: 1 / -1; }
  .line-head { display: none; }
}

/* ---------- Printing (Export PDF) ---------- */

@page { margin: 14mm; }
@media print {
  body { background: #fff; }
  .sidebar, .bottombar, .toasts, .no-print, dialog { display: none !important; }
  .preview-panel { display: block !important; } /* printed even when the Edit tab is showing */
  .main { margin: 0 !important; padding: 0 !important; max-width: none; }
  .split { display: block; }
  .paper { border: 0; box-shadow: none; padding: 0; border-radius: 0; }
}
