:root {
  color-scheme: dark;
  --ink-950: #0a0d12;
  --ink-900: #10151d;
  --ink-800: #161d28;
  --ink-700: #202b3a;
  --ink-600: #2b3a4d;
  --line: #2f4258;
  --parchment: #dce6f0;
  --parchment-dim: #8ea3b8;
  --gold: #c79a4a;
  --gold-bright: #e0b45c;
  --silver: #9fb2c7;
  --steel: #4d7ea8;
  --red: #c1615c;
  --focus: #e0b45c;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --ink-950: #eef2f7;
    --ink-900: #e2eaf2;
    --ink-800: #d3e0ec;
    --ink-700: #bfd2e3;
    --ink-600: #a7bfd6;
    --line: #93aec8;
    --parchment: #0e1620;
    --parchment-dim: #3c5166;
    --gold: #8a651f;
    --gold-bright: #6e4f16;
    --silver: #3d5266;
    --steel: #2c5273;
    --red: #8a3a35;
    --focus: #6e4f16;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink-950: #0a0d12; --ink-900: #10151d; --ink-800: #161d28; --ink-700: #202b3a; --ink-600: #2b3a4d;
  --line: #2f4258; --parchment: #dce6f0; --parchment-dim: #8ea3b8; --gold: #c79a4a; --gold-bright: #e0b45c;
  --silver: #9fb2c7; --steel: #4d7ea8; --red: #c1615c; --focus: #e0b45c;
}
:root[data-theme="light"] {
  color-scheme: light;
  --ink-950: #eef2f7; --ink-900: #e2eaf2; --ink-800: #d3e0ec; --ink-700: #bfd2e3; --ink-600: #a7bfd6;
  --line: #93aec8; --parchment: #0e1620; --parchment-dim: #3c5166; --gold: #8a651f; --gold-bright: #6e4f16;
  --silver: #3d5266; --steel: #2c5273; --red: #8a3a35; --focus: #6e4f16;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--ink-950);
  color: var(--parchment);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}
h1, h2, h3, .display {
  font-family: 'Cinzel', serif;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--parchment);
  margin: 0;
}
a { color: var(--gold-bright); }
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.boot-loading {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; color: var(--parchment-dim); font-family: 'Cinzel', serif;
}

/* ---------- entry screen ---------- */
.entry-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 32px 16px;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(199,154,74,0.08), transparent 60%),
    var(--ink-950);
}
.entry-card {
  width: 100%; max-width: 380px;
  border: 1px solid var(--line);
  background: var(--ink-900);
  padding: 36px 32px;
}
.entry-mark {
  text-align: center; margin-bottom: 28px;
}
.entry-mark .glyph {
  font-size: 28px; color: var(--gold); display: block; margin-bottom: 8px;
}
.entry-mark h1 { font-size: 19px; line-height: 1.25; }
.entry-mark p { color: var(--parchment-dim); font-size: 13px; margin-top: 6px; }
.field { margin-bottom: 16px; }
.field label {
  display: block; font-size: 12.5px; color: var(--parchment-dim); margin-bottom: 6px;
}
.field input, .field select {
  width: 100%; background: var(--ink-800); border: 1px solid var(--line);
  color: var(--parchment); padding: 10px 12px;
}
select, option { background-color: var(--ink-800); color: var(--parchment); }
optgroup { background-color: var(--ink-800); color: var(--parchment-dim); font-style: normal; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--gold); background: transparent; color: var(--gold-bright);
  padding: 10px 18px; font-weight: 600; font-size: 14px; width: 100%;
  transition: background-color 120ms ease, color 120ms ease;
}
.btn:hover { background: var(--gold); color: var(--ink-950); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.small { width: auto; padding: 6px 12px; font-size: 13px; }
.btn.danger { border-color: var(--red); color: var(--red); }
.btn.danger:hover { background: var(--red); color: var(--ink-950); }
.btn.ghost { border-color: var(--line); color: var(--parchment-dim); }
.btn.ghost:hover { background: var(--ink-700); color: var(--parchment); }

.error-msg {
  background: rgba(193,97,92,0.12); border: 1px solid var(--red);
  color: var(--red); padding: 10px 12px; font-size: 13px; margin-bottom: 16px;
}
.hint { color: var(--parchment-dim); font-size: 12.5px; margin-top: 14px; text-align: center; }
.link-btn { background: none; border: none; color: var(--gold-bright); text-decoration: underline; padding: 0; }

/* ---------- app shell ---------- */
.shell { display: flex; min-height: 100vh; }
.rail {
  width: 216px; flex-shrink: 0; background: var(--ink-900); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 20px 0;
}
.rail-brand { padding: 0 20px 20px; border-bottom: 1px solid var(--line); margin-bottom: 12px; }
.rail-brand .glyph { color: var(--gold); font-size: 20px; }
.rail-brand h1 { font-size: 14px; line-height: 1.3; margin-top: 6px; }
.rail-nav { display: flex; flex-direction: column; gap: 2px; padding: 0 10px; flex: 1; }
.rail-nav button {
  text-align: left; background: none; border: none; color: var(--parchment-dim);
  padding: 10px 12px; font-size: 13.5px; border-left: 2px solid transparent;
}
.rail-nav button:hover { color: var(--parchment); background: var(--ink-800); }
.rail-nav button.active {
  color: var(--parchment); background: var(--ink-800); border-left-color: var(--gold);
  font-weight: 600;
}
.rail-nav .badge {
  float: right; background: var(--ink-700); color: var(--parchment-dim);
  font-size: 11px; padding: 1px 7px; border: 1px solid var(--line);
}
.rail-foot { padding: 14px 20px 0; border-top: 1px solid var(--line); margin-top: 12px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 28px; border-bottom: 1px solid var(--line); background: var(--ink-900);
}
.topbar h2 { font-size: 18px; }
.topbar .sub { color: var(--parchment-dim); font-size: 12.5px; margin-top: 3px; }
.who { display: flex; align-items: center; gap: 12px; }
.role-chip {
  display: inline-block; white-space: nowrap;
  font-size: 11px; padding: 3px 9px; border: 1px solid; font-family: 'Inter', sans-serif; font-weight: 600;
}
.role-chip.emperor { color: var(--gold-bright); border-color: var(--gold); }
.role-chip.plenipotentiary { color: var(--silver); border-color: var(--silver); }
.role-chip.master_plenipotentiary { color: var(--ink-950); background: var(--silver); border-color: var(--silver); }
.role-chip.hold_moi { color: var(--steel); border-color: var(--steel); }

.content { padding: 28px; flex: 1; overflow-y: auto; }
.panel { border: 1px solid var(--line); background: var(--ink-900); margin-bottom: 24px; }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.panel-head h3 { font-size: 15px; }
.panel-body { padding: 18px; }
.empty-state { color: var(--parchment-dim); font-size: 13.5px; padding: 20px; text-align: center; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th {
  text-align: left; font-weight: 600; color: var(--parchment-dim); font-size: 12px;
  padding: 8px 10px; border-bottom: 1px solid var(--line);
}
td { padding: 9px 10px; border-bottom: 1px solid var(--ink-700); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.cat-row td { color: var(--parchment-dim); font-size: 11.5px; padding-top: 16px; padding-bottom: 4px; border-bottom: none; }
.manifest-row-flag td { background: rgba(193,97,92,0.08); }
.manifest-row-flag select { background: var(--ink-800); border: 1px solid var(--red); color: var(--parchment); padding: 5px 7px; }
.history-row { display: flex; align-items: center; gap: 10px; padding: 4px 0; font-size: 13px; }
.history-row:not(:last-child) { border-bottom: 1px solid var(--ink-700); }

.search-input {
  width: 100%; max-width: 320px; background: var(--ink-800); border: 1px solid var(--line);
  color: var(--parchment); padding: 8px 12px;
}

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal {
  background: var(--ink-900); border: 1px solid var(--line);
  width: 100%; max-width: 520px; max-height: 85vh; overflow-y: auto; padding: 20px;
}
.modal-wide { max-width: 700px; }

.toggle-switch { position: relative; display: inline-block; width: 42px; height: 23px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle-slider {
  position: absolute; inset: 0; cursor: pointer; background: var(--ink-800);
  border: 1px solid var(--line); transition: background 0.15s, border-color 0.15s;
}
.toggle-slider::before {
  content: ""; position: absolute; height: 15px; width: 15px; left: 3px; top: 3px;
  background: var(--parchment-dim); transition: transform 0.15s, background 0.15s;
}
.toggle-switch input:checked + .toggle-slider { background: var(--gold); border-color: var(--gold); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(19px); background: var(--ink-900); }
.toggle-row { display: flex; align-items: center; gap: 12px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.modal-head h3 { font-size: 16px; }

.qp-scroll { max-height: 48vh; overflow-y: auto; border: 1px solid var(--line); padding: 8px; margin-top: 10px; }
.qp-cat { color: var(--gold); font-size: 11px; margin: 10px 0 5px; }
.qp-cat:first-child { margin-top: 0; }
.qp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--ink-900); }
.qp-cell { background: var(--ink-800); padding: 6px 8px; display: flex; align-items: center; justify-content: space-between; gap: 6px; min-width: 0; }
.qp-name { color: var(--parchment); font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.qp-cell .price-input { width: 58px; font-size: 11.5px; padding: 4px 5px; flex-shrink: 0; }
.qp-cell .qty-input { width: 42px; font-size: 11.5px; padding: 4px 5px; flex-shrink: 0; }

.price-input {
  width: 74px; background: var(--ink-800); border: 1px solid var(--line); color: var(--parchment);
  padding: 5px 7px; text-align: right; font-variant-numeric: tabular-nums;
}
.qty-input {
  width: 64px; background: var(--ink-800); border: 1px solid var(--line); color: var(--parchment);
  padding: 5px 7px; text-align: right;
}

.status-tag { font-size: 11px; padding: 2px 8px; border: 1px solid; display: inline-block; }
.status-tag.pending { color: var(--gold-bright); border-color: var(--gold); }
.status-tag.approved { color: var(--silver); border-color: var(--silver); }
.status-tag.completed { color: var(--steel); border-color: var(--steel); }
.status-tag.rejected { color: var(--red); border-color: var(--red); }
.status-tag.review { color: var(--gold-bright); border-color: var(--gold); border-style: dashed; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); margin-bottom: 24px; }
.stat-cell { background: var(--ink-900); padding: 16px 18px; }
.stat-cell .num { font-family: 'Cinzel', serif; font-size: 24px; color: var(--gold-bright); }
.stat-cell .label { color: var(--parchment-dim); font-size: 12px; margin-top: 4px; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-row .field { margin-bottom: 0; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
textarea { width: 100%; background: var(--ink-800); border: 1px solid var(--line); color: var(--parchment); padding: 10px; resize: vertical; min-height: 60px; }

.order-cart { border: 1px dashed var(--line); padding: 14px; margin-top: 16px; }
.order-cart-line { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; font-size: 13.5px; border-bottom: 1px solid var(--ink-700); }
.order-cart-line:last-of-type { border-bottom: none; }
.order-total { display: flex; justify-content: space-between; font-family: 'Cinzel', serif; font-size: 16px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }

.pill-select { display: flex; gap: 6px; flex-wrap: wrap; }
.pill-select button {
  background: var(--ink-800); border: 1px solid var(--line); color: var(--parchment-dim);
  padding: 6px 12px; font-size: 12.5px;
}
.pill-select button.active { color: var(--ink-950); background: var(--gold); border-color: var(--gold); }

.toast {
  position: fixed; bottom: 22px; right: 22px; background: var(--ink-800);
  border: 1px solid var(--gold); color: var(--parchment); padding: 12px 18px;
  font-size: 13.5px; max-width: 320px; z-index: 50;
}
.toast.error { border-color: var(--red); }

.muted { color: var(--parchment-dim); }
.mt-8 { margin-top: 8px; } .mt-16 { margin-top: 16px; } .mb-0 { margin-bottom: 0; }

@media (max-width: 760px) {
  .shell { flex-direction: column; }
  .rail { width: 100%; flex-direction: row; align-items: center; overflow-x: auto; padding: 10px; }
  .rail-brand { display: none; }
  .rail-nav { flex-direction: row; }
  .rail-foot { display: none; }
  .content { padding: 16px; }
  .form-row { grid-template-columns: 1fr; }
  .qp-grid { grid-template-columns: 1fr; }
  .modal { padding: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
