:root {
  --ink: #12181f;
  --ink-soft: #1c2630;
  --field-dark: #2a3544;
  --copper: #d4894a;
  --copper-deep: #b86a2e;
  --mist: #f3eee6;
  --mist-card: #fffbf5;
  --field-light: #fffcf8;
  --teal: #0f6b6b;
  --danger: #c23b3b;
  --success: #2f8f5b;

  --bg: var(--mist);
  --surface: var(--mist-card);
  --field: var(--field-light);
  --text: var(--ink);
  --muted: rgba(18, 24, 31, .62);
  --border: #e2d8c8;
  --primary: var(--teal);
  --on-primary: #fff;
}

:root[data-theme="dark"] {
  --bg: var(--ink);
  --surface: var(--ink-soft);
  --field: var(--field-dark);
  --text: #f2ede6;
  --muted: rgba(242, 237, 230, .62);
  --border: #3d4a5c;
  --primary: var(--copper);
  --on-primary: var(--ink);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: var(--ink);
    --surface: var(--ink-soft);
    --field: var(--field-dark);
    --text: #f2ede6;
    --muted: rgba(242, 237, 230, .62);
    --border: #3d4a5c;
    --primary: var(--copper);
    --on-primary: var(--ink);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 "Segoe UI", Tahoma, system-ui, -apple-system, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.appbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--bg);
}
.appbar h1 { flex: 1; margin: 0; font-size: 22px; font-weight: 700; }
.appbar-actions { display: flex; gap: 4px; }

.page { max-width: 560px; margin: 0 auto; padding: 8px 20px 40px; }
body.is-hero .page { padding-top: 36px; }

h2, h3 { margin: 18px 0 8px; }
.greeting { font-size: 24px; margin-top: 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.empty { color: var(--muted); text-align: center; padding: 28px 0; }
.error { color: var(--danger); margin: 6px 0; }
.link { color: var(--primary); text-decoration: none; font-weight: 600; }
pre { white-space: pre-wrap; word-break: break-all; font-size: 12px; }

.brand { text-align: center; margin-bottom: 18px; }
.brand-mark {
  width: 68px; height: 68px; margin: 0 auto 12px;
  display: grid; place-items: center;
  border-radius: 22px; background: var(--primary); color: var(--on-primary);
  font-size: 34px; font-weight: 800;
}
.brand-mark.warn { background: var(--danger); color: #fff; }
.brand h1 { margin: 0 0 4px; }
.brand p { margin: 0; color: var(--muted); }

.card {
  display: block;
  background: var(--surface);
  border-radius: 22px;
  padding: 16px 18px;
  margin: 0 0 12px;
  color: inherit;
  text-decoration: none;
}
.card.form, .form { display: flex; flex-direction: column; gap: 14px; }
.card.form { padding: 20px 18px; border: 1px solid var(--border); }
.card.form h2 { margin: 0; }
.details > div { margin-bottom: 4px; }
.info-row { display: flex; align-items: center; gap: 14px; margin-top: 24px; }
.row-link { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.field.grow { flex: 1; min-width: 0; }
input[type=text], input[type=password], input[type=number], select {
  width: 100%; min-height: 52px; padding: 12px 14px;
  background: var(--field); color: var(--text);
  border: 1px solid var(--border); border-radius: 14px;
  font: inherit; font-weight: 400;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.pw { display: flex; align-items: center; gap: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 52px; padding: 0 20px;
  border: 0; border-radius: 14px;
  font: inherit; font-weight: 700; cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-tonal { background: color-mix(in srgb, var(--primary) 18%, transparent); color: var(--text); }
.btn-text { background: none; color: var(--primary); }
.btn-icon { padding: 0 16px; font-size: 22px; background: var(--primary); color: var(--on-primary); }
a.btn-primary { display: flex; margin: 12px 0; }

.icon-btn {
  display: inline-grid; place-items: center;
  min-width: 40px; height: 40px; border: 0; border-radius: 12px;
  background: none; color: var(--text); font-size: 22px; text-decoration: none; cursor: pointer;
}

.lookup { display: flex; align-items: flex-end; gap: 8px; margin: 12px 0; }

.segmented {
  display: flex; margin: 8px 0 16px;
  border: 1px solid var(--border); border-radius: 14px; overflow: hidden;
}
.segmented > * {
  flex: 1; padding: 11px 6px; text-align: center;
  background: none; border: 0; color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.segmented > .on { background: color-mix(in srgb, var(--primary) 22%, transparent); }

.tile {
  display: flex; align-items: center; gap: 14px;
  padding: 16px; margin-bottom: 12px;
  background: var(--surface); border-radius: 22px;
  color: inherit; text-decoration: none;
  border-inline-start: 6px solid var(--accent, var(--primary));
}
.tile-icon { font-size: 28px; }
.tile-text { flex: 1; display: flex; flex-direction: column; }
.tile-text small { color: var(--muted); }
.chev { font-size: 26px; color: var(--muted); }
html[dir=rtl] .chev { transform: scaleX(-1); }
html[dir=rtl] .appbar > .icon-btn { transform: scaleX(-1); }
.accent-teal { --accent: #0f6b6b; }
.accent-copper { --accent: #b86a2e; }
.accent-gold { --accent: #8a6d3b; }
.accent-blue { --accent: #3d6b8e; }
.accent-purple { --accent: #5b4b8a; }

.badge {
  display: inline-block; padding: 1px 9px; border-radius: 99px;
  font-size: 12px; font-weight: 700;
  background: color-mix(in srgb, var(--muted) 25%, transparent);
}
.badge-confirmed { background: color-mix(in srgb, var(--success) 25%, transparent); color: var(--success); }
.badge-rejected { background: color-mix(in srgb, var(--danger) 22%, transparent); color: var(--danger); }

.flash { padding: 12px 14px; border-radius: 14px; margin-bottom: 12px; background: var(--surface); }
.flash-error { color: var(--danger); }
.flash-success { color: var(--success); }

.toast {
  position: fixed; inset-inline: 16px; bottom: max(20px, env(safe-area-inset-bottom));
  max-width: 520px; margin: 0 auto; padding: 14px 16px; z-index: 30;
  background: var(--text); color: var(--bg); border-radius: 14px; text-align: center;
}

.scan-modal {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center;
  background: rgba(0, 0, 0, .7); padding: 16px;
}
.scan-box {
  width: 100%; max-width: 420px; padding: 16px; border-radius: 22px;
  background: var(--surface); display: flex; flex-direction: column; gap: 12px;
}
#scan-reader { width: 100%; min-height: 220px; overflow: hidden; border-radius: 14px; }

[hidden] { display: none !important; }

.btn-sm { min-height: 40px; padding: 0 14px; font-size: 14px; }
.appbar .btn-sm { white-space: nowrap; }
a.btn-tonal { display: flex; margin-bottom: 4px; }

/* Excel-like grid */
.sheet-wrap {
  overflow: auto; margin: 4px -20px 16px; padding: 0 20px;
  max-height: 70vh; border-radius: 6px;
}
.sheet { border-collapse: collapse; font-size: 14px; background: #fff; color: #1f1f1f; min-width: 100%; }
.sheet th, .sheet td { border: 1px solid #d0d0d0; padding: 4px 10px; white-space: nowrap; height: 28px; }
.sheet .letters th { background: #e6e6e6; color: #444; font-weight: 600; text-align: center; position: sticky; top: 0; }
.sheet .rn { background: #e6e6e6; color: #444; font-weight: 600; text-align: center; min-width: 38px; position: sticky; inset-inline-start: 0; }
.sheet .corner { background: #d4d4d4; position: sticky; top: 0; inset-inline-start: 0; z-index: 2; }
.sheet .meta .lbl { font-weight: 700; background: #f4f4f4; }
.sheet .head td { background: #d9e8e8; font-weight: 700; }
.sheet td.num { text-align: right; font-variant-numeric: tabular-nums; }
.accent-green { --accent: #2f8f5b; }
.filters { display: flex; gap: 8px; margin-bottom: 12px; }
.filters select { min-height: 44px; }
.sheet a { color: #0f6b6b; font-weight: 600; text-decoration: none; }

/* DataTable */
.dt-card { padding: 14px; }
.dt-top, .dt-bottom { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin: 4px 0 12px; font-size: 14px; }
.dt-bottom { margin: 12px 0 0; }
.dt-len select, .dt-search input { min-height: 36px; width: auto; padding: 4px 10px; border-radius: 8px; }
.dt-search input { min-width: 160px; }
.dt-scroll { overflow-x: auto; }
table.dt { width: 100%; border-collapse: collapse; font-size: 14px; }
table.dt th, table.dt td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.dt thead th { border-bottom: 2px solid var(--border); font-weight: 700; position: relative; padding-inline-end: 22px; user-select: none; }
table.dt th.dt-sortable { cursor: pointer; }
table.dt th.dt-sortable::after { content: '\21C5'; position: absolute; inset-inline-end: 6px; opacity: .3; font-size: 12px; }
table.dt th.asc::after { content: '\25B2'; opacity: .9; }
table.dt th.desc::after { content: '\25BC'; opacity: .9; }
table.dt tbody tr:nth-child(odd) { background: color-mix(in srgb, var(--text) 4%, transparent); }
table.dt tbody tr:hover { background: color-mix(in srgb, var(--primary) 12%, transparent); }
table.dt td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.dt a { color: var(--primary); font-weight: 600; text-decoration: none; }
table.dt .dt-empty td { text-align: center; color: var(--muted); padding: 22px; }
.dt-pager { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.dt-btn { min-width: 34px; height: 34px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; background: none; color: var(--text); font: inherit; font-size: 13px; cursor: pointer; }
.dt-btn.on { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.dt-btn:disabled { opacity: .4; cursor: default; }
.dt-gap { padding: 0 4px; color: var(--muted); }

/* ---- wide data pages / tidy data table ---- */
.page.wide { max-width: 1180px; }
.filters select { width: auto; min-width: 170px; flex: 0 1 220px; }
.dt-top { justify-content: space-between; }
.dt-len, .dt-search { display: flex; align-items: center; gap: 8px; margin: 0; font-weight: 600; }
.dt-len select { width: 72px; }
.dt-search input { width: 220px; max-width: 100%; border: 1px solid var(--border); background: var(--field); color: var(--text); font: inherit; }
.dt-bottom { justify-content: space-between; }
table.dt td, table.dt th { vertical-align: middle; }
table.dt td a { text-decoration: none; }
table.dt td a:hover { text-decoration: underline; }
@media (max-width: 600px) {
  .page.wide { padding-inline: 12px; }
  .dt-top, .dt-bottom { flex-direction: column; align-items: stretch; }
  .dt-search input { width: 100%; }
  .filters { flex-direction: column; }
  .filters select { width: 100%; flex: none; }
}
.dt-scroll { direction: ltr; }
.dt-card { min-width: 0; }
.btn-danger { background: var(--danger); color: #fff; width: 100%; }
.clear-form { margin-top: 10px; }

.sel-col { width: 40px; text-align: center !important; }
table.dt input[type=checkbox] { width: 18px; height: 18px; min-height: 0; accent-color: var(--primary); cursor: pointer; }
.btn-danger:disabled { background: var(--border); color: var(--muted); }

.accent-rose { --accent: #b04a6b; }

/* ---- Data Entry grid ---- */
.grid2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.grid3 .span2 { grid-column: span 2; }
@media (max-width: 700px) { .grid2, .grid3 { grid-template-columns: 1fr; } .grid3 .span2 { grid-column: auto; } }
.entry-summary { display: flex; flex-wrap: wrap; gap: 8px 24px; margin: 4px 0 10px; font-size: 15px; }
.entry-toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.entry-toolbar .btn { min-height: 40px; cursor: pointer; }
.entry-wrap { overflow: auto; direction: ltr; max-height: 62vh; border: 1px solid #c8c8c8; background: #fff; margin-bottom: 18px; }
.entry-grid { border-collapse: collapse; font-size: 14px; color: #1f1f1f; }
.entry-grid th, .entry-grid td { border: 1px solid #d6d6d6; padding: 0; height: 30px; }
.entry-grid .letters th { background: #e6e6e6; color: #555; font-weight: 600; position: sticky; top: 0; z-index: 2; height: 24px; }
.entry-grid .titles th { background: #d9e8e8; font-weight: 700; padding: 4px 8px; white-space: nowrap; position: sticky; top: 24px; z-index: 2; text-align: left; }
.entry-grid .rn { background: #e6e6e6; color: #555; text-align: center; min-width: 36px; position: sticky; left: 0; }
.entry-grid input, .entry-grid select { width: 100%; min-height: 0; height: 30px; padding: 2px 8px; border: 0; border-radius: 0; background: transparent; color: #1f1f1f; font: inherit; }
.entry-grid input:focus, .entry-grid select:focus { outline: 2px solid #0f6b6b; outline-offset: -2px; background: #f3fbfb; }
.entry-grid input[readonly] { background: #f4f4f4; color: #555; }
.entry-grid td.del { border: 0; background: transparent; padding-inline: 4px; }
.entry-grid .icon-btn { min-width: 28px; height: 28px; font-size: 14px; color: #b33; }
.row-actions form { display: inline; }
.mini { padding: 4px 10px; border: 1px solid var(--border); border-radius: 8px; background: none; color: var(--text); font: inherit; font-size: 13px; cursor: pointer; }
.mini-danger { color: var(--danger); }
.accent-indigo { --accent: #4a5bb0; }
.entry-grid .icon-btn.plus { color: #0f6b6b; font-size: 18px; font-weight: 700; }
.entry-grid td.del { white-space: nowrap; }
