:root {
  color-scheme: dark;
  --font: "Noto Sans Thai", "Sarabun", "Segoe UI", system-ui, -apple-system, sans-serif;
  --bg: #0f141b; --panel: #151b24; --panel-2: #1b2330; --chip: #27313f;
  --line: #242d3a; --line-strong: #39465a; --grid: #1a212c;
  --text: #e6ebf2; --muted: #a3afc0; --faint: #8b99ad;
  --accent: #4c9ffe; --accent-ink: #06121f;
  --up: #2fbf84; --down: #f0717c; --together: #2fbf84; --opposite: #f0616d; --cell-none: #202935; --cell-together: #17805a; --cell-opposite: #ad3a46;
  --event: #f0b429; --alt-line: #f0b429; --recession: rgba(147,160,178,.16);
  --pair-a: #4c9ffe; --pair-b: #f0b429; --warn: #f0b429; --aiw: 460px; --tab-h: 56px;
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #fff; --panel: #f6f7f9; --panel-2: #eceff3; --chip: #dfe4eb;
  --line: #e1e5ea; --line-strong: #c4cbd5; --grid: #f1f3f6;
  --text: #17202c; --muted: #4f5b6b; --faint: #5c6775;
  --accent: #1565c0; --accent-ink: #fff;
  --up: #066b45; --down: #b02935; --together: #0d9a65; --opposite: #d6404d; --cell-none: #eef0f3; --cell-together: #52c49c; --cell-opposite: #f08e96;
  --event: #c98a00; --alt-line: #c98a00; --recession: rgba(90,103,120,.14); --pair-a: #1a6fd6; --pair-b: #c98a00; --warn: #8a5a00;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 13px/1.45 var(--font); overflow: hidden; overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }
h2, h3, h4, p { margin: 0; }

#shell { display: flex; height: 100dvh; }
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
#top { display: flex; align-items: center; gap: 8px; padding: 4px 8px; padding-top: calc(4px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--line); background: var(--panel); }
#views { display: flex; gap: 2px; flex: none; }
#views button { background: none; border: 0; padding: 5px 10px; border-radius: 6px; color: var(--muted); font-weight: 600; white-space: nowrap; }
#views button.on { background: var(--chip); color: var(--text); }
#controls { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 3px 6px; }
#controls::-webkit-scrollbar { display: none; }
#topRight { display: flex; align-items: center; gap: 6px; flex: none; }
#updated { color: var(--faint); font-size: 11px; white-space: nowrap; }
#body { flex: 1; min-height: 0; display: flex; }
#stage { flex: 1; min-width: 0; position: relative; overflow: auto; }
#tabbar { display: none; }

.seg { display: inline-flex; flex: none; background: var(--panel-2); border-radius: 7px; padding: 2px; gap: 1px; }
.seg button { background: none; border: 0; padding: 3px 7px; border-radius: 5px; color: var(--muted); white-space: nowrap; }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--chip); color: var(--text); font-weight: 600; }
.btn { border: 1px solid var(--line-strong); background: none; border-radius: 7px; padding: 5px 12px; white-space: nowrap; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.ghost { color: var(--text); }
.btn.tool { flex: none; padding: 3px 9px; color: var(--muted); }
.btn.tool.on { color: var(--text); background: var(--chip); border-color: var(--chip); }
.btn.danger, .menu-item.danger { color: var(--down); }
.btn.armed, .icon-btn.armed { background: var(--down); border-color: var(--down); color: #fff; }
.btn:disabled { opacity: .55; }
.icon-btn { background: none; border: 0; color: var(--muted); width: 30px; height: 30px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.armed { width: auto; padding: 0 8px; }
input:not([type=color]):not([type=range]), select, textarea { background: var(--bg); border: 1px solid var(--line-strong); border-radius: 7px; padding: 5px 8px; min-width: 0; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Rail */
#rail { width: 286px; flex: none; display: flex; flex-direction: column; border-left: 1px solid var(--line); background: var(--panel); min-height: 0; }
.rail-head { display: flex; gap: 6px; padding: 6px; }
.rail-search { flex: 1; width: 100%; }
.rail-done { display: none; }
.presets { display: flex; gap: 4px; padding: 0 6px 6px; overflow-x: auto; scrollbar-width: none; flex: none; }
.presets::-webkit-scrollbar { display: none; }
.presets button { flex: none; border: 1px solid var(--line); background: none; color: var(--muted); padding: 2px 9px; border-radius: 999px; font-size: 12px; }
.presets button:hover { color: var(--text); border-color: var(--line-strong); }
.rail-list { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.rail-group { display: flex; align-items: center; position: sticky; top: 0; z-index: 1; background: var(--panel-2); }
.rail-gname { flex: 1; text-align: left; background: none; border: 0; padding: 4px 8px; color: var(--muted); font-size: 12px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.chev { width: 0; height: 0; border-left: 5px solid currentColor; border-top: 4px solid transparent; border-bottom: 4px solid transparent; transition: transform .12s; }
.chev.open { transform: rotate(90deg); }
.rail-gall { background: none; border: 0; color: var(--faint); font-size: 11px; padding: 4px 8px; }
.rail-gall.on { color: var(--accent); }
.rail-item { display: grid; grid-template-columns: 10px 1fr auto 58px; align-items: center; gap: 6px; width: 100%; background: none; border: 0; border-bottom: 1px solid var(--grid); padding: 4px 8px; text-align: left; }
.rail-item:hover { background: var(--panel-2); }
.rail-item.on { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.dot { width: 9px; height: 9px; border-radius: 50%; border: 1px solid var(--line-strong); }
.rail-item.on .dot { border-color: transparent; }
.rail-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rail-last { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.rail-ch { text-align: right; font-size: 12px; font-variant-numeric: tabular-nums; color: var(--faint); }
.up { color: var(--up) !important; }
.down { color: var(--down) !important; }

/* Chart board */
.board { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr)); grid-auto-rows: minmax(220px, 1fr); gap: 1px; background: var(--line); overflow-y: auto; }
.cell { position: relative; background: var(--bg); min-height: 0; min-width: 0; }
.cell-chart { position: absolute; inset: 0; }
.legend { position: absolute; top: 4px; left: 6px; z-index: 3; pointer-events: none; display: flex; flex-direction: column; gap: 0; font-size: 12px; max-width: calc(100% - 70px); }
.legend.static { position: static; flex-direction: row; flex-wrap: wrap; gap: 2px 12px; padding: 4px 2px; max-width: none; }
.lg-title { font-weight: 700; color: var(--muted); }
.lg-row { display: flex; align-items: baseline; gap: 5px; white-space: nowrap; line-height: 1.5; }
.lg-row i { align-self: center; width: 8px; height: 8px; border-radius: 2px; flex: none; }
.lg-row b { font-weight: 600; font-variant-numeric: tabular-nums; }
.lg-name { color: var(--muted); }
.lg-ch { font-variant-numeric: tabular-nums; }
.lg-axis { color: var(--faint); font-size: 10px; }
.lg-ind { display: flex; flex-wrap: wrap; gap: 0 8px; font-size: 11px; }
.lg-ohlc { color: var(--faint); font-size: 11px; font-variant-numeric: tabular-nums; }
.legend.many { flex-direction: row; flex-wrap: wrap; gap: 0 10px; max-height: 42%; overflow: hidden; }
.legend.many .lg-ch, .legend.many .lg-axis { display: none; }
.legend.many .lg-date { width: 100%; }
.lg-date { color: var(--faint); font-size: 11px; font-variant-numeric: tabular-nums; }
.event-tip { position: absolute; left: 50%; transform: translateX(-50%); bottom: 34px; z-index: 4; pointer-events: none; background: var(--panel-2); border: 1px solid var(--line-strong); border-radius: 8px; padding: 5px 10px; max-width: min(90%, 420px); display: flex; flex-direction: column; font-size: 12px; }
.event-tip b { color: var(--event); }
.tooltip { position: absolute; z-index: 5; pointer-events: none; background: var(--panel-2); border: 1px solid var(--line-strong); border-radius: 8px; padding: 5px 9px; font-size: 12px; box-shadow: 0 4px 14px rgba(0,0,0,.35); white-space: nowrap; }
.tt-date { color: var(--muted); font-size: 11px; margin-bottom: 2px; font-variant-numeric: tabular-nums; }
.tt-row { display: flex; align-items: center; gap: 6px; line-height: 1.55; }
.tt-row i { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.tt-name { flex: 1; color: var(--muted); margin-right: 8px; }
.tt-row b { font-weight: 600; font-variant-numeric: tabular-nums; }
.tt-row small { color: var(--faint); width: 32px; text-align: right; font-variant-numeric: tabular-nums; }
.empty { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; height: 100%; color: var(--muted); }
.phone-only { display: none; }

/* Relationships */
/* Bottom space lets the last item scroll clear of the floating AI character */
.rel-page, .pair-page { padding: 10px 12px 130px; max-width: 1280px; margin: 0 auto; }
.rel-hint { color: var(--muted); font-size: 12px; margin: 4px 0 10px; }
.rel-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; }
.rel-card { text-align: left; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; gap: 7px; color: var(--text); }
.rel-card:hover { border-color: var(--line-strong); }
.rel-card-head { display: flex; justify-content: space-between; gap: 6px; align-items: flex-start; }
.rel-why { color: var(--muted); font-size: 12px; }
.rel-chips { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.r-chip { border-radius: 6px; padding: 2px 6px; font-weight: 700; font-variant-numeric: tabular-nums; display: flex; justify-content: space-between; align-items: baseline; color: var(--text); }
.r-chip small { font-weight: 400; color: var(--text); font-size: 11px; }
.badge { font-size: 10px; border: 1px solid var(--line-strong); border-radius: 999px; padding: 0 7px; white-space: nowrap; color: var(--muted); }
.badge.warn { border-color: var(--warn); color: var(--warn); }
.spark { width: 100%; height: 34px; display: block; }
.spark-zero { stroke: var(--line-strong); stroke-dasharray: 3 3; stroke-width: 1; vector-effect: non-scaling-stroke; }
.spark-line { fill: none; stroke: var(--accent); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.top-pairs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; margin-bottom: 10px; }
.top-pairs > div { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.top-pairs small { width: 100%; color: var(--muted); }
.pair-chip { border: 0; border-radius: 6px; padding: 3px 8px; color: var(--text); font-size: 12px; }
.heat-wrap { overflow: auto; max-height: 72dvh; border: 1px solid var(--line); border-radius: 8px; }
.heat { border-collapse: separate; border-spacing: 1px; background: var(--bg); }
.heat th { position: sticky; background: var(--panel); font-weight: 500; font-size: 12px; z-index: 1; color: var(--muted); }
.heat thead th { top: 0; }
.heat thead th:first-child { left: 0; z-index: 3; }
.heat-col { height: 96px; vertical-align: bottom; padding: 4px 0; min-width: 34px; }
.heat-col span { writing-mode: vertical-rl; transform: rotate(180deg); white-space: nowrap; margin: 0 auto; display: inline-block; }
.heat-row { left: 0; text-align: right; padding: 0 8px; white-space: nowrap; z-index: 2; }
.heat td { width: 34px; height: 32px; min-width: 34px; text-align: center; font-size: 11px; font-variant-numeric: tabular-nums; cursor: pointer; }
.heat td.diag { cursor: default; background: var(--panel-2) !important; }
.heat td:not(.diag):hover { outline: 2px solid var(--text); outline-offset: -2px; }
.rel-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; margin: 10px 0 4px; color: var(--muted); font-size: 12px; }
.swatch { width: 18px; height: 12px; border-radius: 3px; margin-left: 8px; }
.map-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.map-tools input { flex: 1; min-width: 140px; max-width: 320px; }
.map-label { color: var(--muted); font-size: 12px; }
.map-host { display: flex; justify-content: center; }
.rel-map { width: 100%; max-width: 680px; height: auto; touch-action: manipulation; overflow: visible; }
.map-edge { cursor: pointer; }
.map-edge.dim { opacity: .06; }
.map-node { cursor: pointer; }
.map-node circle:nth-of-type(2) { fill: var(--panel-2); stroke: var(--muted); stroke-width: 1.5; }
.map-node.on circle:nth-of-type(2) { fill: var(--accent); stroke: var(--accent); }
.map-node text { fill: var(--text); font-size: 15px; }
.pair-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.pair-head h2 { flex: 1; font-size: 16px; min-width: 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; display: flex; flex-direction: column; }
.tile b { font-size: 20px; font-variant-numeric: tabular-nums; line-height: 1.25; }
.tile small { color: var(--muted); }
.tile.main { border-color: transparent; }
.pair-chart { margin: 4px 0 12px; }
.pair-chart-host { position: relative; height: min(58dvh, 520px); min-height: 320px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.era { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin-bottom: 10px; }
.era h3 { font-size: 13px; margin-bottom: 6px; }
.era-scroll { overflow-x: auto; }
.era-bars { display: flex; justify-content: center; gap: 2px; min-width: min-content; height: 92px; }
.era-col, .lag-col { flex: 1 0 15px; max-width: 42px; display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 15px; }
.era-track { position: relative; flex: 1; width: 100%; background: linear-gradient(var(--line-strong), var(--line-strong)) center / 100% 1px no-repeat; }
.era-track i { position: absolute; left: 12%; right: 12%; border-radius: 2px; }
.era-col small, .lag-col small { color: var(--faint); font-size: 9px; }
.lag-text { margin-bottom: 6px; }
.lag-bars { display: flex; justify-content: center; gap: 2px; height: 70px; }
.lag-axis { display: flex; justify-content: space-between; color: var(--faint); font-size: 11px; margin-top: 2px; }
.ask-ai { width: 100%; padding: 10px; }
.loading { color: var(--muted); padding: 16px; }

/* Pop */
#sheetBackdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 8000; }
#pop { z-index: 8700; background: var(--panel); }
#pop.popover { position: fixed; border: 1px solid var(--line-strong); border-radius: 10px; padding: 10px; box-shadow: 0 8px 26px rgba(0,0,0,.45); max-height: 80dvh; overflow-y: auto; max-width: min(460px, calc(100vw - 16px)); }
.settings { display: flex; flex-direction: column; gap: 10px; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.set-row label { color: var(--muted); }
.date-pop { display: flex; flex-direction: column; gap: 8px; min-width: 240px; }
.date-pop h4, .ind-pop h4 { font-size: 13px; color: var(--muted); font-weight: 600; }
.date-row { display: flex; align-items: center; gap: 6px; }
.date-row input { flex: 1; width: 110px; }
.ind-pop { display: flex; flex-direction: column; gap: 8px; }
.ind-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 4px; }
.ind-btn { display: flex; flex-direction: column; text-align: left; background: var(--panel-2); border: 1px solid transparent; border-radius: 7px; padding: 4px 8px; }
.ind-btn:hover { border-color: var(--line-strong); }
.ind-btn small { color: var(--muted); font-size: 11px; line-height: 1.25; }
.ind-item { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; border-top: 1px solid var(--line); padding-top: 6px; }
.ind-item > b { flex: 1 0 auto; }
.ind-item input[type=color] { width: 28px; height: 28px; padding: 0; border: 0; background: none; flex: none; }
.ind-param { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 12px; }
.ind-param input { width: 62px; }
.menu { display: flex; flex-direction: column; min-width: 190px; }
.menu-who { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 4px 8px 0; }
.menu-who small, .menu-updated { color: var(--faint); }
.menu-updated { padding: 0 8px 6px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-item { background: none; border: 0; text-align: left; padding: 7px 8px; border-radius: 6px; }
.menu-item:hover { background: var(--panel-2); }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(70px + env(safe-area-inset-bottom, 0px)); z-index: 9500; background: var(--chip); color: var(--text); padding: 8px 14px; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.4); max-width: 90vw; }
.fatal { position: fixed; inset: 0; z-index: 9600; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 20px; text-align: center; background: var(--bg); }

/* AI assistant: same character, left dock and draggable right edge as the POS panel */
/* Clear of the chart library's attribution logo at the bottom left */
#aiBtn { position: fixed; left: 62px; bottom: 26px; z-index: 7500; background: none; border: 0; padding: 0; line-height: 0; touch-action: none; filter: drop-shadow(0 3px 8px rgba(0,0,0,.4)); }
#aiBtn img { width: 96px; height: 96px; display: block; pointer-events: none; user-select: none; -webkit-user-drag: none; }
body.ai-open #aiBtn { display: none; }
#aiPanel { display: none; flex: none; position: relative; width: var(--aiw); min-width: 300px; max-width: calc(100vw - 300px); background: var(--panel); border-right: 1px solid var(--line); }
#aiPanel.on { display: flex; }
.ai-col { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
#aiGrip { width: 7px; flex: none; cursor: ew-resize; background: var(--chip); touch-action: none; }
#aiGrip:hover { background: var(--accent); }
body.resizing { cursor: ew-resize; user-select: none; }
body.resizing .cell-chart { pointer-events: none; }
.ai-head { display: flex; align-items: center; gap: 4px; padding: 4px 6px; padding-top: calc(4px + env(safe-area-inset-top, 0px)); border-bottom: 1px solid var(--line); min-height: 50px; }
.ai-face { background: none; border: 0; padding: 0; line-height: 0; flex: none; }
.ai-face img { width: 42px; height: 42px; pointer-events: none; }
.cmark { width: 15px; height: 15px; flex: none; display: inline-block; }
.cmark svg { width: 100%; height: 100%; display: block; }
.ai-title { flex: 1; min-width: 0; background: none; border: 0; text-align: left; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 6px 4px; }
.ai-list { position: absolute; top: 50px; left: 0; right: 0; bottom: 0; background: var(--panel); z-index: 5; overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 2px; }
.ai-new { background: none; border: 1px dashed var(--line-strong); border-radius: 8px; padding: 8px; margin-bottom: 4px; flex: none; }
.ai-empty { color: var(--muted); padding: 10px; text-align: center; }
.ai-row { display: flex; align-items: center; gap: 2px; border-radius: 8px; }
.ai-row.on { background: var(--panel-2); }
.ai-row-main { flex: 1; min-width: 0; background: none; border: 0; text-align: left; padding: 6px 8px; display: flex; flex-direction: column; }
.ai-row-main b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-row-main small { color: var(--faint); font-size: 11px; }
.ai-rename { flex: 1; }
.ai-msgs { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 10px; display: flex; flex-direction: column; gap: 10px; }
.ai-older { background: none; border: 1px solid var(--line-strong); border-radius: 8px; padding: 6px 10px; color: var(--muted); flex: none; }
.msg { max-width: 92%; }
.msg.user { align-self: flex-end; background: color-mix(in srgb, var(--accent) 18%, var(--panel-2)); border-radius: 12px 12px 3px 12px; padding: 6px 10px; }
.msg.user .msg-body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.ai { align-self: stretch; max-width: 100%; }
.msg-body { overflow-wrap: anywhere; line-height: 1.6; font-size: 14px; }
.msg-body p { margin: 0 0 8px; }
.msg-body ul, .msg-body ol { margin: 0 0 8px; padding-left: 20px; }
.msg-body h1, .msg-body h2, .msg-body h3 { font-size: 14px; margin: 10px 0 4px; }
.msg-body pre { background: var(--bg); border-radius: 6px; padding: 8px; overflow-x: auto; font-size: 12px; }
.msg-body table { border-collapse: collapse; display: block; overflow-x: auto; margin-bottom: 8px; font-size: 13px; }
.msg-body th, .msg-body td { border: 1px solid var(--line-strong); padding: 3px 7px; white-space: nowrap; }
.chart-link { display: block; width: 100%; text-align: left; background: color-mix(in srgb, var(--accent) 14%, transparent); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 10px; margin: 4px 0 8px; color: var(--text); }
.msg-note { color: var(--faint); font-size: 12px; }
.msg-note.live::after { content: ''; display: inline-block; width: 6px; height: 6px; margin-left: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .2; } }
@media (prefers-reduced-motion: reduce) { .msg-note.live::after { animation: none; } .chev { transition: none; } }
.msg-err { color: var(--down); font-size: 12px; margin-top: 4px; }
.ai-welcome { display: flex; flex-direction: column; gap: 6px; color: var(--muted); }
.ai-welcome p { margin-bottom: 6px; }
.ai-starter { background: var(--panel-2); border: 0; border-radius: 8px; padding: 8px 10px; text-align: left; color: var(--text); }
.ai-compose { display: flex; gap: 6px; align-items: flex-end; padding: 6px; padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--line); }
.ai-compose textarea { flex: 1; resize: none; max-height: 160px; line-height: 1.45; font-size: 14px; }
.ai-send { background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 7px; padding: 6px 14px; font-weight: 600; flex: none; }
.ai-send.stop { background: var(--down); color: #fff; }

/* Admin */
#adminView { position: fixed; inset: 0; z-index: 8800; background: var(--bg); display: flex; flex-direction: column; padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); }
.adm-top { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.adm-body { flex: 1; min-height: 0; overflow-y: auto; padding: 12px; max-width: 1000px; width: 100%; margin: 0 auto; }
.adm-add { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.adm-add input { flex: 1 1 150px; }
.adm-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.adm-who { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; }
.adm-who small { width: 100%; color: var(--faint); }
.adm-actions { display: flex; flex-wrap: wrap; gap: 4px; }
.adm-role { color: var(--muted); }
.adm-pass { display: flex; gap: 6px; width: 100%; }
.adm-pass input { flex: 1; }
.adm-scroll { overflow-x: auto; }
.adm-table { border-collapse: collapse; width: 100%; font-size: 12px; }
.adm-table th, .adm-table td { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.adm-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.adm-table tr.bad td { color: var(--down); }

/* Login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100dvh; overflow: auto; padding: 16px; }
.login-card { width: min(340px, 100%); display: flex; flex-direction: column; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px; }
.login-card img { border-radius: 12px; align-self: center; }
.login-card h1 { font-size: 20px; margin: 0; text-align: center; }
.login-card label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
.login-card input { padding: 10px; font-size: 16px; color: var(--text); }
.login-card label.remember { flex-direction: row; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; color: var(--text); }
.login-card label.remember input { width: 22px; height: 22px; min-height: 0; flex: none; accent-color: var(--accent); }
.login-card .btn { padding: 11px; }
.err { color: var(--down); margin: 0; }

/* Narrow toolbar, e.g. a tablet with the assistant open: one row of buttons, the rest under Settings */
#main.compact #top { flex-wrap: wrap; }
#main.compact #views { order: 1; }
#main.compact #topRight { order: 2; margin-left: auto; }
#main.compact #updated { display: none; }
#main.compact #controls { order: 3; flex-basis: 100%; flex-wrap: nowrap; }
#main.compact #controls.rel-controls { flex-wrap: wrap; }
#main.compact #controls .seg { flex: 0 1 auto; min-width: 0; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
#main.compact #controls .seg::-webkit-scrollbar { display: none; }
#main.compact #controls .seg button { flex: none; }
#main.compact #controls .btn.tool { flex: none; }

/* Touch: a fingertip needs about 44px, whatever the screen size */
@media (pointer: coarse) {
  .seg button, #views button { min-height: 40px; padding: 6px 12px; }
  .btn, .btn.tool { min-height: 40px; padding: 6px 12px; }
  .icon-btn { width: 44px; height: 44px; font-size: 16px; }
  .rail-item { min-height: 46px; }
  .rail-gname, .rail-gall { min-height: 40px; }
  .presets button { min-height: 36px; padding: 4px 12px; }
  .menu-item { min-height: 44px; }
  .ind-btn { min-height: 48px; }
  .ai-row-main { min-height: 48px; }
  .ai-title { min-height: 44px; }
  .ai-send { min-height: 44px; }
  input:not([type=color]):not([type=range]), select, textarea { min-height: 42px; font-size: 16px; }
  .ind-item input[type=color] { width: 40px; height: 40px; }
  #aiGrip { width: 16px; }
  .pair-chip { min-height: 36px; }
  .heat td { width: 38px; height: 38px; min-width: 38px; }
}

/* Phone portrait */
@media (max-width: 760px), (max-height: 500px) {
  #views, #updated, #aiBtn { display: none; }
  #top { padding-inline: 8px; gap: 6px; }
  /* Only the button strips scroll sideways; the settings button must always stay on screen */
  #controls { flex-wrap: nowrap; }
  #controls .seg { flex: 0 1 auto; min-width: 0; overflow-x: auto; scrollbar-width: none; }
  #controls .seg::-webkit-scrollbar { display: none; }
  #controls .seg button { flex: none; padding-inline: 8px; }
  #controls .btn.tool { flex: none; }
  #controls.rel-controls { flex-wrap: wrap; }
  #controls.rel-controls .seg { max-width: 100%; }
  #stage { padding-bottom: 0; }
  .phone-only { display: inline-block; }
  #tabbar { display: grid; grid-template-columns: repeat(4, 1fr); flex: none; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom, 0px); }
  #tabbar button { background: none; border: 0; min-height: var(--tab-h); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; font-size: 11px; color: var(--muted); }
  #tabbar button.on { color: var(--accent); }
  #tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  #tabbar img { width: 28px; height: 28px; }

  .board { grid-auto-rows: minmax(240px, 1fr); }
  .board.n1 { grid-auto-rows: 1fr; }
  .board:not(.n1) { grid-auto-rows: max-content; }
  .board:not(.n1) .cell { height: 46dvh; min-height: 240px; }
  .board:not(.n1) .cell.tall { height: 66dvh; }
  .legend { font-size: 11px; max-width: calc(100% - 56px); }

  #rail { position: fixed; left: 0; right: 0; bottom: 0; width: auto; height: 86dvh; border-left: 0; border-top: 1px solid var(--line-strong); border-radius: 14px 14px 0 0; z-index: 8100; transform: translateY(105%); transition: transform .18s ease-out; padding-bottom: env(safe-area-inset-bottom, 0px); visibility: hidden; }
  #rail.open { transform: none; visibility: visible; }
  .rail-done { display: inline-block; }
  .rail-item { grid-template-columns: 12px 1fr auto 66px; font-size: 14px; }

  #pop.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 82dvh; overflow-y: auto; overscroll-behavior: contain; border-radius: 14px 14px 0 0; border-top: 1px solid var(--line-strong); padding: 8px 12px calc(14px + env(safe-area-inset-bottom, 0px)); }
  .sheet-grab { display: flex; justify-content: flex-end; position: sticky; top: 0; background: var(--panel); padding-bottom: 6px; z-index: 1; }
  .date-pop { min-width: 0; margin-top: 4px; }
  .ind-grid { grid-template-columns: repeat(2, 1fr); }

  /* The assistant takes the whole phone screen, with no resizing */
  #aiPanel { position: fixed; inset: 0; width: 100% !important; max-width: none; min-width: 0; z-index: 8500; border: 0; height: 100dvh; }
  #aiGrip { display: none; }

  .rel-page, .pair-page { padding: 8px 10px 20px; }
  .rel-cards { grid-template-columns: 1fr; }
  /* The strongest pairs stay one swipeable row each, so the table starts near the top */
  .top-pairs { grid-template-columns: minmax(0, 1fr); gap: 4px; margin-bottom: 4px; }
  .top-pairs > div { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; align-items: stretch; }
  .top-pairs > div::-webkit-scrollbar { display: none; }
  .top-pairs small { width: auto; flex: none; align-self: center; margin-right: 2px; }
  .pair-chip { flex: none; white-space: nowrap; }
  .rel-legend { margin: 4px 0; }
  .legend.static .lg-row { white-space: normal; }
  .heat-wrap { max-height: 64dvh; }
  .pair-chart-host { height: 46dvh; min-height: 290px; }
  .tiles { grid-template-columns: 1fr 1fr; }
  .tile b { font-size: 18px; }
  .map-node text { font-size: 25px; }
  .adm-add .btn { flex: 1 1 auto; }
}

/* Tablets and small laptops: give the charts room */
@media (min-width: 761px) and (max-width: 1100px) and (min-height: 501px) {
  /* With the assistant open there is no room for three columns: the chart comes first */
  body.ai-open #rail { display: none; }
  #rail { width: 230px; }
  .rail-item { grid-template-columns: 10px 1fr 52px; }
  .rail-last { display: none; }
}

/* Phone held sideways: every row of height counts */
@media (max-height: 500px) {
  #tabbar button { min-height: 44px; flex-direction: row; gap: 6px; font-size: 12px; }
  #tabbar svg { width: 18px; height: 18px; }
  #tabbar img { width: 22px; height: 22px; }
  #rail { height: 94dvh; }
  #pop.sheet { max-height: 94dvh; }
  .ai-head { min-height: 44px; }
  .ai-list { top: 44px; }
  .ai-face img { width: 34px; height: 34px; }
  .pair-chart-host { height: 82dvh; }
}
