:root {
  --p:  #060852;  /* Primary navy */
  --p2: #1a1875;  /* Navy hover */
  --p3: #3730a3;  /* Indigo accent */
  --p4: #a5b4fc;  /* Lavender */
  --p5: #e0e7ff;  /* Very light indigo */
  --bg: #f8f8f8;
  --s:  #ffffff;
  --bd: #dde1f5;
  --t:  #1e293b;
  --m:  #6b7280;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --ok: #047857;
  --ok-bg: #d1fae5;

  /* TV display theme tokens */
  --d-bg: #05071f;
  --d-text: #e8eaf8;
  --d-surface: rgba(255,255,255,.045);
  --d-border: rgba(165,180,252,.18);
  --d-border-soft: rgba(165,180,252,.12);
  --d-accent: #a5b4fc;
  --d-highlight: rgba(165,180,252,.22);
  --d-highlight-strong: rgba(165,180,252,.28);
  --d-alarm: #f87171;

  /* Brine tank colours */
  --tank-a: #1d4ed8;
  --tank-b: #6d28d9;
  --tank-c: #047857;
  --tank-d: #b45309;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'DM Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--t);
  font-size: 14px;
  line-height: 1.45;
}
.mono { font-family: 'DM Mono', monospace; }

::selection { background: var(--p5); color: var(--p); }
:focus-visible { outline: 2px solid var(--p3); outline-offset: 2px; }

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--p); color: #fff; padding: .6rem 1rem; border-radius: 0 0 8px 0;
  font-weight: 600; text-decoration: none;
}
.skip-link:focus { left: 0; }

/* ---------------------------------------------------------------- app shell */
#app { display: flex; min-height: 100vh; }

#sidebar {
  background: var(--p); width: 232px; flex-shrink: 0;
  display: flex; flex-direction: column; padding: 0 0 1rem;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
#sidebar-header {
  background: #fff; border-bottom: 3px solid var(--p);
  padding: .8rem 1rem; display: flex; align-items: center; gap: .5rem;
}
#sidebar-header img { height: 40px; width: auto; }
.sidebar-footer {
  margin-top: auto; padding: 1rem 1.1rem 0;
  display: flex; align-items: center; gap: .5rem;
  color: rgba(255,255,255,.45); font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .6px;
}
.sidebar-footer .credit-chip {
  background: #fff; border-radius: 6px; padding: 3px 8px; display: inline-flex; align-items: center;
}
#sidebar .app-name {
  color: rgba(255,255,255,.9); font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .8px;
  padding: 1rem 1.1rem .4rem;
}
.nav-group-label {
  color: rgba(255,255,255,.4); font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .6px; padding: .9rem 1.1rem .25rem;
}
.chip {
  display: flex; align-items: center; gap: .55rem;
  margin: 2px .6rem; padding: .5rem .6rem; border-radius: 8px;
  background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.06);
  color: rgba(255,255,255,.75); text-decoration: none; font-weight: 500;
  transition: all .15s; cursor: pointer; font-size: 13px;
}
.chip:hover { background: rgba(255,255,255,.13); color: #fff; }
.chip.active { background: rgba(165,180,252,.18); border-color: rgba(165,180,252,.45); color: #fff; }
.chip:focus-visible { outline-color: var(--p4); }
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--p4); flex-shrink: 0; }

#main { flex: 1; padding: 1.4rem 1.8rem 3rem; min-width: 0; }

.top-bar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.3rem; gap: 1rem; flex-wrap: wrap; }
.section-label { font-size: 11px; color: var(--m); font-weight: 600; text-transform: uppercase; letter-spacing: .6px; }
h1 { font-size: 21px; font-weight: 700; color: var(--p); margin: 0; }
h2 { font-size: 15px; font-weight: 600; color: var(--p); margin: 0 0 .7rem; }
h3 { font-size: 13px; font-weight: 600; margin: 0 0 .4rem; }

.card {
  background: var(--s); border: 1px solid var(--bd); border-radius: 14px;
  padding: 1.25rem 1.4rem; box-shadow: 0 1px 3px rgba(6,8,82,.05);
  margin-bottom: 1.1rem;
}
.card.tight { padding: .9rem 1rem; }
.cards-row { display: flex; gap: 1.1rem; flex-wrap: wrap; }
.cards-row > .card { flex: 1; min-width: 260px; margin-bottom: 0; }

.info-banner {
  background: var(--p5); border-left: 3px solid var(--p3); border-radius: 8px;
  padding: .85rem 1rem; font-size: 13px; color: var(--p2); margin-bottom: 1rem;
}
.warn-banner {
  background: var(--warn-bg); border-left: 3px solid var(--warn); border-radius: 8px;
  padding: .85rem 1rem; font-size: 13px; color: var(--warn); margin-bottom: 1rem; font-weight: 600;
}
.danger-banner {
  background: var(--danger-bg); border-left: 3px solid var(--danger); border-radius: 8px;
  padding: .85rem 1rem; font-size: 13px; color: var(--danger); margin-bottom: 1rem; font-weight: 600;
}

.badge {
  background: var(--p5); color: var(--p3); font-size: 10px; font-weight: 700;
  padding: 3px 10px; border-radius: 20px; text-transform: uppercase; letter-spacing: .4px;
  display: inline-block;
}
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }

button, .btn, .btn-primary {
  font-family: 'DM Sans', sans-serif; font-size: 13px; cursor: pointer;
  transition: all .15s; border-radius: 8px; padding: .5rem 1rem; font-weight: 600;
}
.btn { background: var(--s); color: var(--t); border: 1.5px solid var(--bd); }
.btn:hover { background: var(--p5); border-color: var(--p4); }
.btn-primary { background: var(--p); color: #fff; border: 1.5px solid var(--p); }
.btn-primary:hover { background: var(--p2); border-color: var(--p2); }
.btn.small, .btn-primary.small { padding: .28rem .6rem; font-size: 12px; }
.btn.danger { color: var(--danger); border-color: #fca5a5; }
.btn.danger:hover { background: var(--danger-bg); }

input, select, textarea {
  border: 1px solid var(--bd); border-radius: 8px; padding: .42rem .6rem;
  font-family: 'DM Sans', sans-serif; font-size: 13px; color: var(--t); background: #fff;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--p3); box-shadow: 0 0 0 3px rgba(55,48,163,.1); outline: none;
}
input[aria-invalid="true"] {
  border-color: var(--danger); box-shadow: 0 0 0 3px rgba(185,28,28,.12);
}
input.time { font-family: 'DM Mono', monospace; width: 84px; }
input.num { width: 74px; font-family: 'DM Mono', monospace; }
label.field { display: flex; flex-direction: column; gap: .25rem; font-size: 12px; font-weight: 600; color: var(--m); }
.fields { display: flex; gap: .9rem; flex-wrap: wrap; align-items: flex-end; }

/* ------------------------------------------------------------------ PIN gate */
.pin-gate {
  min-height: 100vh; width: 100%; display: flex; align-items: center; justify-content: center;
  background: var(--p); padding: 1rem;
}
.pin-card {
  background: var(--s); border-radius: 14px; padding: 2.2rem 2.4rem;
  box-shadow: 0 12px 40px rgba(0,0,0,.35);
  display: flex; flex-direction: column; align-items: center; gap: .9rem;
  text-align: center; max-width: 320px; width: 100%;
}

/* ------------------------------------------------------------------ dialogs */
dialog.app-dialog {
  border: 1px solid var(--bd); border-radius: 14px; padding: 1.4rem 1.5rem;
  box-shadow: 0 12px 40px rgba(6,8,82,.25); max-width: 420px; width: calc(100vw - 2rem);
  font-family: 'DM Sans', system-ui, sans-serif; color: var(--t);
}
dialog.app-dialog::backdrop { background: rgba(6,8,82,.35); }
dialog.app-dialog h2 { margin-bottom: .4rem; }
dialog.app-dialog p { margin: 0 0 1rem; font-size: 13.5px; color: var(--m); }
dialog.app-dialog .dialog-error { color: var(--danger); font-size: 12.5px; font-weight: 600; margin: .35rem 0 0; min-height: 1em; }
dialog.app-dialog .dialog-actions { display: flex; gap: .6rem; justify-content: flex-end; margin-top: 1.1rem; }
dialog.app-dialog input { width: 100%; }

/* -------------------------------------------------------------------- tables */
table.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
table.grid th {
  text-align: left; font-size: 10.5px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--m); font-weight: 700; padding: .45rem .55rem; border-bottom: 2px solid var(--bd);
  white-space: nowrap; position: sticky; top: 0; background: var(--s); z-index: 1;
}
table.grid td { padding: .4rem .55rem; border-bottom: 1px solid var(--bd); white-space: nowrap; }
table.grid tr:hover td { background: var(--p5); }
table.grid td.time { font-family: 'DM Mono', monospace; font-variant-numeric: tabular-nums; }
table.grid td.txt { font-weight: 600; }
table.grid td.locked { color: var(--p3); font-weight: 700; }
table.grid tr.current td { background: var(--p5); font-weight: 600; }
table.grid tr.past td { color: var(--m); opacity: .65; }
table.grid td .cellbtn {
  background: none; border: none; padding: 0; font: inherit; cursor: pointer;
  font-family: 'DM Mono', monospace; color: inherit; border-bottom: 1px dashed transparent;
}
table.grid td .cellbtn:hover { border-bottom-color: var(--p3); color: var(--p3); }
table.grid td input.time-edit { width: 92px; padding: .15rem .3rem; font-family: 'DM Mono', monospace; font-size: 13px; }
.table-scroll { overflow-x: auto; }

/* ------------------------------------------------------------------ planner */
.planner-toolbar { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.vat-row-warn { background: var(--warn-bg) !important; }
td.warn-cell { color: var(--warn); font-weight: 700; }
td.tank-A { color: var(--tank-a); font-weight: 700; }
td.tank-B { color: var(--tank-b); font-weight: 700; }
td.tank-C { color: var(--tank-c); font-weight: 700; }
td.tank-D { color: var(--tank-d); font-weight: 700; }

/* ------------------------------------------------------------ TV display view */
body.display-mode { background: var(--d-bg); overflow: hidden; }
.display-root {
  height: 100vh; width: 100vw; display: flex; flex-direction: column;
  padding: 1.2vmin 1.6vmin; gap: 1vmin;
  font-size: calc(10px * var(--scale, 1) + .9vmin);
}
.display-root.theme-dark,
.display-root.theme-custom { background: var(--d-bg); color: var(--d-text); }
.display-root.theme-light { background: var(--bg); color: var(--t); }

.logo-widget { display: flex; align-items: center; height: 100%; }
.logo-widget.align-left { justify-content: flex-start; }
.logo-widget.align-center { justify-content: center; }
.logo-widget.align-right { justify-content: flex-end; }
.logo-widget img { max-width: 100%; object-fit: contain; }
.logo-placeholder { opacity: .5; font-size: .8em; }

.color-field { display: flex; align-items: center; gap: .4rem; }
.color-field input[type="color"] {
  width: 34px; height: 30px; padding: 2px; border: 1px solid var(--bd);
  border-radius: 6px; background: #fff; cursor: pointer;
}
.color-field input.hex { width: 88px; font-family: 'DM Mono', monospace; }
.logo-upload-preview {
  border: 1px dashed var(--bd); border-radius: 8px; padding: .5rem;
  background: repeating-conic-gradient(#eee 0% 25%, #fff 0% 50%) 0 0 / 14px 14px;
  margin-bottom: .5rem; text-align: center;
}
.logo-upload-preview img { max-width: 100%; max-height: 72px; }
.display-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.display-brand { display: flex; align-items: center; gap: .7em; min-width: 0; }
/* the wordmark is dark ink, so it sits in a white chip to stay legible on
   dark and custom display themes */
.display-logo {
  height: 1.6em; width: auto; flex-shrink: 0;
  background: #fff; border-radius: .3em; padding: .15em .35em;
}
.display-header .screen-name {
  font-size: 1.15em; font-weight: 700; text-transform: uppercase; letter-spacing: .12em;
  color: var(--d-accent);
}
.theme-light .display-header .screen-name { color: var(--p3); }
.display-header .plan-date { font-size: .85em; opacity: .7; }
.display-grid {
  flex: 1; display: grid; grid-template-columns: repeat(12, 1fr);
  gap: 1.1vmin; align-content: start; overflow: hidden; min-height: 0;
}
.dwidget {
  background: var(--d-surface); border: 1px solid var(--d-border);
  border-radius: 12px; padding: 1vmin 1.3vmin; overflow: hidden;
  display: flex; flex-direction: column; min-height: 0;
}
.theme-light .dwidget { background: var(--s); border-color: var(--bd); box-shadow: 0 1px 3px rgba(6,8,82,.05); }
.dwidget .w-label {
  font-size: .55em; font-weight: 700; text-transform: uppercase; letter-spacing: .12em;
  opacity: .68; margin-bottom: .2em;
}
.dwidget .w-body { flex: 1; min-height: 0; overflow: hidden; }
.dwidget .w-body.scrolly { overflow-y: auto; scrollbar-width: none; }

.big-time { font-family: 'DM Mono', monospace; font-size: 3.2em; font-weight: 500; line-height: 1.05; font-variant-numeric: tabular-nums; }
.big-time.huge { font-size: 6.5em; }
.big-value { font-family: 'DM Mono', monospace; font-size: 2.1em; font-weight: 500; line-height: 1.1; font-variant-numeric: tabular-nums; }
.big-name { font-size: 1.35em; font-weight: 700; color: var(--d-accent); }
.theme-light .big-name { color: var(--p3); }
.sub-note { font-size: .75em; opacity: .68; }

.dtable { border-collapse: collapse; width: 100%; font-size: .95em; }
.dtable th {
  text-align: left; font-size: .55em; text-transform: uppercase; letter-spacing: .1em;
  opacity: .68; padding: .25em .5em; position: sticky; top: 0;
  background: inherit;
}
.dtable td { padding: .22em .5em; border-top: 1px solid var(--d-border-soft); font-family: 'DM Mono', monospace; font-variant-numeric: tabular-nums; }
.theme-light .dtable td { border-top-color: var(--bd); }
.dtable td.txt { font-family: 'DM Sans', sans-serif; font-weight: 600; }
.dtable tr.past { opacity: .38; }
.dtable tr.current { background: var(--d-highlight); }
.dtable tr.current td { font-weight: 700; }
.theme-light .dtable tr.current { background: var(--p5); }

.brine-bands { display: flex; flex-direction: column; gap: .6vmin; height: 100%; overflow-y: auto; }
.brine-band { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: .35vmin; }
.brine-cell {
  border: 1px solid var(--d-border); border-radius: 6px; padding: .3em .1em;
  text-align: center; font-size: .66em; line-height: 1.3; min-width: 0;
}
.theme-light .brine-cell { border-color: var(--bd); background: #fff; }
.brine-cell .bt { font-weight: 700; }
.brine-cell .bin, .brine-cell .bout {
  font-family: 'DM Mono', monospace; display: block; font-size: .92em; opacity: .8;
  white-space: nowrap; overflow: hidden;
}
.brine-cell.current { background: var(--d-highlight-strong); border-color: var(--d-accent); }
.brine-cell.done { opacity: .3; }

.guard-ok { font-size: 1.6em; font-weight: 700; }
.guard-bad { font-size: 1.6em; font-weight: 800; color: var(--d-alarm); animation: blink 1.2s step-end infinite; }
@keyframes blink { 50% { opacity: .35; } }

/* ----------------------------------------------------------------- designer */
.designer-layout { display: flex; gap: 1.1rem; align-items: flex-start; }
.designer-panel { width: 330px; flex-shrink: 0; }
.designer-preview { flex: 1; min-width: 0; }
.preview-frame {
  border: 2px solid var(--bd); border-radius: 12px; overflow: hidden;
  aspect-ratio: 16 / 9; position: relative; background: var(--d-bg);
}
.preview-frame .display-root { position: absolute; inset: 0; height: 100%; width: 100%; font-size: calc(5px * var(--scale, 1) + .45vmin); }
.widget-item {
  display: flex; align-items: center; gap: .5rem; padding: .45rem .6rem;
  border: 1px solid var(--bd); border-radius: 8px; margin-bottom: .4rem; background: #fff;
}
.widget-item.selected { border-color: var(--p3); box-shadow: 0 0 0 3px rgba(55,48,163,.1); }
.widget-item .w-type { font-weight: 600; flex: 1; }
.widget-item button { padding: .15rem .4rem; font-size: 12px; }
.screen-tabs { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.screen-tab {
  padding: .4rem .85rem; border-radius: 20px; border: 1.5px solid var(--bd);
  background: #fff; cursor: pointer; font-weight: 600; font-size: 13px; transition: all .15s;
}
.screen-tab:hover { border-color: var(--p4); background: var(--p5); }
.screen-tab.active { background: var(--p); color: #fff; border-color: var(--p); }

/* batch cards */
.batch-card { border: 2px solid var(--p); border-radius: 14px; padding: 1.2rem 1.4rem; margin-bottom: 1rem; background: #fff; break-inside: avoid; }
.batch-card h2 { display: flex; justify-content: space-between; }
.batch-card table { font-size: 13px; }
.batch-card td { padding: .18rem .8rem .18rem 0; }

/* ------------------------------------------------------------- responsive */
@media (max-width: 900px) {
  #app { flex-direction: column; }
  #sidebar {
    width: 100%; height: auto; position: static; padding-bottom: .6rem;
  }
  #sidebar .nav-scroll {
    display: flex; overflow-x: auto; padding: 0 .4rem; gap: .1rem;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  #sidebar .app-name { padding: .6rem 1rem .3rem; }
  .nav-group-label { display: none; }
  .sidebar-footer { display: none; } /* top-bar mode: credit would sit mid-page */
  .chip { flex-shrink: 0; white-space: nowrap; margin: 2px .15rem; }
  #main { padding: 1rem .9rem 2.5rem; }
  .designer-layout { flex-direction: column; }
  .designer-panel { width: 100%; }
  input, select, textarea { font-size: 16px; } /* prevents iOS focus zoom */
  input.time { width: 104px; }
  input.num { width: 84px; }
}

@media (min-width: 901px) {
  #sidebar .nav-scroll { display: block; }
}

@media (pointer: coarse) {
  button, .btn, .btn-primary, .chip, .screen-tab { min-height: 44px; }
  .btn.small, .btn-primary.small, .widget-item button { min-height: 38px; padding: .35rem .7rem; }
  table.grid td .cellbtn { padding: .5rem 0; }
}

/* -------------------------------------------------------- reduced motion */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .guard-bad {
    animation: none;
    background: rgba(248, 113, 113, .16);
    outline: 3px solid var(--d-alarm);
    border-radius: 8px; padding: .1em .35em;
  }
}

@media print {
  #sidebar, .planner-toolbar, .skip-link, .no-print { display: none !important; }
  #main { padding: 0; }
  body { background: #fff; }
  .card { box-shadow: none; }
}
