:root {
  --ink: #1F3864;        /* navy: brand, navigation, headings */
  --ink-2: #2b4a80;
  --paper: #FBFAF7;      /* warm white background */
  --sand: #E8E2D5;       /* hairlines, surfaces */
  --sand-2: #F3EFE6;
  --gold: #B8860B;       /* used sparingly: active marker, hero rule */
  --text: #1c2333;
  --muted: #646a78;
  --gain: #2f6b4f;
  --loss: #9b2c2c;
  --warn: #8a5a00;
  --radius: 10px;
  --serif: "Playfair Display", Georgia, "Times New Roman", serif;
  --sans: "Manrope", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --nav-w: 232px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--paper); color: var(--text); font: 15px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--ink); }
h1, h2, h3 { font-family: var(--serif); color: var(--ink); font-weight: 600; margin: 0; }
h1 { font-size: 1.75rem; line-height: 1.2; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.gain { color: var(--gain); } .loss { color: var(--loss); }
.boot { padding: 4rem; text-align: center; color: var(--muted); }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* Shell ---------------------------------------------------------------- */
.shell { display: grid; grid-template-columns: var(--nav-w) 1fr; min-height: 100vh; }
.nav { background: var(--ink); color: #e9ecf3; padding: 1.5rem 0 1rem; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; }
.brand { padding: 0 1.4rem 1.6rem; font-family: var(--serif); }
.brand .ar { font-size: 2rem; line-height: 1; color: #fff; }
.brand .en { font-size: .82rem; letter-spacing: .08em; color: #b9c3d8; margin-top: .35rem; }
.nav a { display: flex; align-items: center; gap: .7rem; padding: .62rem 1.4rem; color: #d5dbe8; text-decoration: none; border-left: 3px solid transparent; }
.nav a:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav a.active { border-left-color: var(--gold); color: #fff; background: rgba(255,255,255,.07); }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav .spacer { flex: 1; }
.nav .who { padding: .8rem 1.4rem 0; font-size: .82rem; color: #b9c3d8; border-top: 1px solid rgba(255,255,255,.1); }
.main { min-width: 0; padding: 2rem clamp(1rem, 3vw, 2.5rem) 4rem; max-width: 1240px; }
.topbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
.topbar .actions { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.tabbar { display: none; }

/* Controls ------------------------------------------------------------- */
.btn { border: 1px solid var(--sand); background: #fff; padding: .5rem .9rem; border-radius: 8px; cursor: pointer; line-height: 1.2; white-space: nowrap; }
.btn:hover { border-color: #cfc6b3; }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary:hover { background: var(--ink-2); }
.btn.danger { color: var(--loss); }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.link { border: 0; background: none; padding: 0; color: var(--ink); text-decoration: underline; text-underline-offset: 2px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { border: 1px solid var(--sand); background: #fff; border-radius: 6px; padding: .5rem .6rem; width: 100%; min-width: 0; }
textarea { min-height: 4.5rem; resize: vertical; }
label { display: block; font-size: .85rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: .25rem; color: var(--text); font-size: .95rem; }
.grid { display: grid; gap: .9rem; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
.check { display: flex; align-items: center; gap: .5rem; color: var(--text); font-size: .95rem; }
.check input { width: auto; }
.filters { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.filters > * { width: auto; min-width: 140px; flex: 0 1 auto; }
.filters input[type=search] { min-width: 220px; flex: 1 1 220px; }
.chip { display: inline-flex; align-items: center; gap: .35rem; background: var(--sand-2); border: 1px solid var(--sand); border-radius: 999px; padding: .2rem .6rem; font-size: .82rem; }
.chip button { border: 0; background: none; cursor: pointer; padding: 0; color: var(--muted); }
.badge { display: inline-block; font-size: .72rem; padding: .08rem .45rem; border-radius: 999px; background: var(--sand-2); color: var(--muted); border: 1px solid var(--sand); vertical-align: 1px; }
.badge.repeat { background: #eef1f8; color: var(--ink); border-color: #d6dcea; }
.badge.warn { background: #fbf3e2; color: var(--warn); border-color: #efdcb3; }
.badge.bad { background: #f8eaea; color: var(--loss); border-color: #ecd0d0; }
.seg { display: inline-flex; border: 1px solid var(--sand); border-radius: 8px; overflow: hidden; background: #fff; }
.seg button { border: 0; background: none; padding: .45rem .8rem; cursor: pointer; }
.seg button.on { background: var(--ink); color: #fff; }

/* Panels & tables ------------------------------------------------------ */
.panel { background: #fff; border: 1px solid var(--sand); border-radius: var(--radius); padding: 1.1rem 1.2rem; }
.panel + .panel, .stack > * + * { margin-top: 1rem; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; margin-bottom: .8rem; }
.table-wrap { overflow-x: auto; border: 1px solid var(--sand); border-radius: var(--radius); background: #fff; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: .6rem .75rem; border-bottom: 1px solid var(--sand-2); vertical-align: top; }
th { font-size: .78rem; font-weight: 600; color: var(--muted); background: #fdfcf9; position: sticky; top: 0; }
td.r, th.r { text-align: right; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--sand-2); }
tfoot td { font-weight: 600; border-top: 1px solid var(--sand); border-bottom: 0; }
.empty { padding: 2.5rem 1rem; text-align: center; color: var(--muted); }
.cards { display: none; }

/* Overview -------------------------------------------------------------- */
.hero { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 1.5rem; align-items: center; padding: 1.6rem 0 1.8rem; border-bottom: 1px solid var(--sand); margin-bottom: 1.5rem; }
.hero .label { color: var(--muted); font-size: .95rem; }
.hero button.figure, .hero button.sub { display: block; border: 0; background: none; padding: 0; text-align: left; cursor: pointer; }
.hero button.sub:hover, .hero button.figure:hover { text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 4px; }
.hero .figure { font-family: var(--serif); font-size: clamp(2.6rem, 6vw, 4.2rem); line-height: 1.05; color: var(--ink); font-variant-numeric: lining-nums tabular-nums; margin-top: .3rem; }
.hero .figure small { font-size: .38em; color: var(--muted); font-family: var(--sans); margin-left: .35rem; }
.hero .rule { width: 56px; height: 2px; background: var(--gold); margin: .9rem 0 .7rem; }
.hero .sub { color: var(--muted); }
.small { font-size: .85rem; }
.hero .sub strong { color: var(--text); font-weight: 600; }
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0; border: 1px solid var(--sand); border-radius: var(--radius); background: #fff; overflow: hidden; }
.kpi { padding: .85rem 1rem; border: 0; border-right: 1px solid var(--sand-2); border-bottom: 1px solid var(--sand-2); background: none; text-align: left; cursor: pointer; }
.kpi:nth-child(2n) { border-right: 0; }
.kpi:hover { background: var(--sand-2); }
.kpi .k { font-size: .8rem; color: var(--muted); }
.kpi .v { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.kpi .d { font-size: .78rem; }
.cols { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 1rem; }
.rank { list-style: none; margin: 0; padding: 0; }
.rank li { border-bottom: 1px solid var(--sand-2); }
.rank li:last-child { border-bottom: 0; }
.rank li > button { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .2rem .8rem; width: 100%; padding: .5rem 0; border: 0; background: none; text-align: left; cursor: pointer; min-height: 2.5rem; }
.rank li > button:hover .n { text-decoration: underline; }
.rank .bar { grid-column: 1 / -1; height: 4px; background: var(--sand-2); border-radius: 2px; overflow: hidden; }
.rank .bar i { display: block; height: 100%; background: var(--ink); opacity: .75; }
.chart-box { position: relative; height: 280px; }
.attention li { cursor: pointer; }

/* Order sheet ------------------------------------------------------------ */
.breakdown { display: grid; grid-template-columns: 1fr auto; gap: .25rem 1rem; font-size: .92rem; }
.breakdown .total { font-weight: 700; border-top: 1px solid var(--sand); padding-top: .35rem; margin-top: .2rem; }
.line-item { border: 1px solid var(--sand); border-radius: 8px; padding: .75rem .85rem; }
.line-item + .line-item { margin-top: .6rem; }
.line-item header { display: flex; justify-content: space-between; gap: .5rem; align-items: baseline; margin-bottom: .55rem; }

/* Modal ----------------------------------------------------------------- */
.modal-bg { position: fixed; inset: 0; background: rgba(20, 28, 48, .42); display: flex; justify-content: flex-end; z-index: 50; }
.modal { background: var(--paper); width: min(640px, 100%); height: 100%; overflow-y: auto; padding: 1.4rem 1.5rem 6rem; box-shadow: -10px 0 40px rgba(20,28,48,.18); }
.modal.center { margin: auto; height: auto; max-height: 92vh; border-radius: 12px; width: min(460px, 94%); padding-bottom: 1.5rem; }
.modal-bg:has(.modal.center) { justify-content: center; }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1.2rem; }
.modal-foot { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1.2rem; flex-wrap: wrap; }
.close { border: 0; background: none; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); }
#toast { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: .6rem 1rem; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.error { background: var(--loss); }

/* Login ------------------------------------------------------------------ */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; background: var(--ink); }
.login form { width: min(380px, 100%); background: var(--paper); border-radius: 14px; padding: 2rem; }
.login .ar { font-family: var(--serif); font-size: 2.4rem; color: var(--ink); line-height: 1; }
.login .rule { width: 40px; height: 2px; background: var(--gold); margin: .8rem 0 1.4rem; }

/* Mobile ---------------------------------------------------------------- */
@media (max-width: 860px) {
  .shell { display: block; }
  .nav { display: none; }
  .main { padding: 1rem 1rem calc(5.5rem + env(safe-area-inset-bottom)); }
  .topbar { margin-bottom: 1rem; }
  h1 { font-size: 1.45rem; }
  .tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; bottom: 0; left: 0; right: 0; background: var(--ink); padding-bottom: env(safe-area-inset-bottom); z-index: 40; }
  .tabbar a { color: #c9d1e2; text-decoration: none; font-size: .68rem; display: flex; flex-direction: column; align-items: center; gap: .15rem; padding: .55rem 0 .5rem; border-top: 2px solid transparent; }
  .tabbar a.active { color: #fff; border-top-color: var(--gold); }
  .tabbar svg { width: 20px; height: 20px; }
  .hero { grid-template-columns: 1fr; padding-top: .5rem; }
  .cols, .g3, .g4 { grid-template-columns: 1fr; }
  .g2 { grid-template-columns: minmax(0, 1fr); }
  .span2 { grid-column: auto; }
  .table-wrap.has-cards { display: none; }
  .cards { display: block; }
  .card { background: #fff; border: 1px solid var(--sand); border-radius: var(--radius); padding: .8rem .9rem; margin-bottom: .6rem; }
  .card.click { cursor: pointer; }
  .card .row { display: flex; justify-content: space-between; gap: .75rem; }
  .card .row + .row { margin-top: .2rem; }
  .filters > * { flex: 1 1 45%; min-width: 0; }
  .modal { padding: 1.1rem 1rem 6rem; }
  .modal.center { width: 94%; }
}
@media (max-width: 860px) {
  /* Comfortable tap targets on phones. */
  .btn, .seg button, .tabbar a, input, select { min-height: 44px; }
  .btn.small { min-height: 40px; }
  .btn.link { min-height: 0; padding: .4rem 0; }
  .chip button { min-width: 32px; min-height: 32px; }
  .close { min-width: 44px; min-height: 44px; }
  .table-wrap { -webkit-overflow-scrolling: touch; }
  /* Tables marked .stack become labelled cards on phones (labels come from the header row). */
  table.stack thead { display: none; }
  table.stack tr { display: block; padding: .6rem .8rem; border-bottom: 1px solid var(--sand-2); }
  table.stack td { display: flex; justify-content: space-between; align-items: center; gap: .75rem; border: 0; padding: .2rem 0; text-align: right; }
  table.stack td::before { content: attr(data-label); color: var(--muted); font-size: .82rem; text-align: left; flex: none; max-width: 45%; }
  table.stack td:first-child { font-weight: 600; }
  table.stack td input, table.stack td select { max-width: 60%; }
  table.stack td:empty { display: none; }
}
.chips { align-items: center; }
.btn[aria-busy="true"] { opacity: .6; cursor: progress; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
.cols > .panel { margin-top: 0; }
.breakdown > :nth-child(2n) { text-align: right; }
