:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1d2330;
  --muted: #6b7280;
  --line: #e2e5ea;
  --accent: #3b5bdb;
  --accent-ink: #ffffff;
  --pro: #2f9e44;
  --pro-bg: #ebfbee;
  --neu: #868e96;
  --neu-bg: #f1f3f5;
  --con: #e03131;
  --con-bg: #fff0f0;
  --pool-bg: #eef2ff;
  --tile: #ffffff;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --panel: #1a1e26;
    --ink: #e7e9ee;
    --muted: #9aa1ad;
    --line: #2b313c;
    --accent: #5c7cfa;
    --pro: #51cf66;
    --pro-bg: #15291b;
    --neu: #adb5bd;
    --neu-bg: #20252e;
    --con: #ff6b6b;
    --con-bg: #2e1819;
    --pool-bg: #1c2236;
    --tile: #232833;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.danger-text { color: var(--con); }
.muted { color: var(--muted); font-weight: normal; }
.err { color: var(--con); margin: 0; }

/* Gate */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.gate-card {
  background: var(--panel); padding: 32px; border-radius: 16px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 12px; width: min(360px, 100%); text-align: center;
}
.gate-card h1 { margin: 0; font-size: 22px; }
.gate-card p { margin: 0; color: var(--muted); }
.gate-card .err { color: var(--con); }
.gate-icon { font-size: 40px; }
.gate-card input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }

/* Top bar */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 17px; }
.boards { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.boards select { padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); max-width: 260px; }
.top-right { display: flex; gap: 6px; align-items: center; }
.sync { font-size: 13px; color: var(--muted); min-width: 64px; text-align: right; }
.sync.err { color: var(--con); }

/* Verdict bar */
.verdict { padding: 12px 16px 0; }
.verdict-inner {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 10px 14px; display: grid; grid-template-columns: 1fr auto 1fr; gap: 12px; align-items: center;
}
.vbar { height: 10px; border-radius: 99px; background: var(--neu-bg); overflow: hidden; display: flex; }
.vbar.l { justify-content: flex-end; }
.vbar span { display: block; height: 100%; border-radius: 99px; transition: width .3s; }
.vbar.l span { background: var(--muted); }
.vbar.r span { background: var(--accent); }
.vtext { text-align: center; font-weight: 600; white-space: nowrap; }
.vtext small { display: block; font-weight: normal; color: var(--muted); font-size: 12px; }

.verdict-inner.breaker { outline: 2px solid var(--con); }
.vtext .vwarn { display: block; color: var(--con); font-size: 13px; }

/* Gut feel */
.gut { padding: 10px 16px 0; }
.gut-row {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 10px 14px; display: grid; grid-template-columns: 1fr minmax(0, 1.2fr) 1fr; gap: 12px; align-items: center;
}
.gut-side { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; align-items: center; font-size: 13px; color: var(--muted); }
.gut-side span { grid-column: 1 / -1; }
.gut-side b { color: var(--ink); }
.gut-side input { width: 100%; accent-color: var(--accent); }
.gut-side output { font-weight: 700; color: var(--ink); min-width: 34px; text-align: right; }
.gut-side.unset output { color: var(--muted); font-weight: normal; }
.gut-side.unset input { opacity: .5; }
.gut-msg { text-align: center; font-size: 13px; color: var(--muted); }
.gut-msg.warn { color: #b7791f; font-weight: 600; }

/* Facts */
.facts { margin: 10px 16px 0; background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); }
.facts > summary { cursor: pointer; padding: 10px 14px; font-weight: 600; }
.facts-inner { padding: 0 14px 12px; }
.table-scroll { overflow-x: auto; }
.facts-table { width: 100%; border-collapse: collapse; min-width: 520px; }
.facts-table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 6px; border-bottom: 1px solid var(--line); }
.facts-table td { padding: 3px 4px; border-bottom: 1px solid var(--line); }
.facts-table input, .facts-table select {
  width: 100%; border: 1px solid transparent; background: transparent; border-radius: 6px; padding: 5px 6px;
}
.facts-table input:hover, .facts-table input:focus, .facts-table select:hover { border-color: var(--line); outline: none; background: var(--bg); }
.facts-table td.win input, .facts-table td.win { background: var(--pro-bg); color: var(--pro); font-weight: 600; }
.facts-table .lbl input { font-weight: 600; }
.facts-table .col-better { width: 110px; }
.facts-table td:last-child { width: 30px; text-align: center; }
.fact-del { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.fact-del:hover { color: var(--con); }
.calc td { background: var(--pool-bg); padding: 7px 10px; font-variant-numeric: tabular-nums; }
.calc td.lbl { font-weight: 600; }
.calc td.win { background: var(--pro-bg); }
.calc tr.big td { font-size: 16px; font-weight: 700; }
.calc .sub { color: var(--muted); font-size: 12px; font-weight: normal; }
.facts-actions { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; padding-top: 10px; font-size: 14px; }
.inline { display: flex; align-items: center; gap: 6px; }
.inline select { border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; background: var(--panel); }
.calc-note { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

/* Main grid */
.grid {
  display: grid; grid-template-columns: 1fr minmax(240px, 300px) 1fr; gap: 16px;
  padding: 16px; align-items: start;
}
.side, .middle {
  background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); padding: 12px;
  display: flex; flex-direction: column; gap: 10px; min-width: 0;
}
.middle { position: sticky; top: 70px; max-height: calc(100vh - 86px); overflow: auto; }

.side-head { display: flex; align-items: center; gap: 8px; }
.side-tag { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; background: var(--neu-bg); color: var(--muted); padding: 3px 8px; border-radius: 99px; }
.side-tag.new { background: var(--accent); color: var(--accent-ink); }
.side-title {
  flex: 1; min-width: 0; font-size: 18px; font-weight: 700; border: 1px solid transparent;
  background: transparent; padding: 4px 6px; border-radius: 6px;
}
.side-title:hover, .side-title:focus { border-color: var(--line); outline: none; }
.side-score { font-weight: 700; font-size: 18px; min-width: 40px; text-align: right; }

.zone-wrap { border-radius: var(--radius); padding: 8px; }
.zone-wrap h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; display: flex; justify-content: space-between; }
.zone-wrap.pro { background: var(--pro-bg); } .zone-wrap.pro h3 { color: var(--pro); }
.zone-wrap.neu { background: var(--neu-bg); } .zone-wrap.neu h3 { color: var(--neu); }
.zone-wrap.con { background: var(--con-bg); } .zone-wrap.con h3 { color: var(--con); }
.zone-wrap.pool { background: var(--pool-bg); } .zone-wrap.pool h3 { color: var(--accent); }
.zone-wrap.trash { border: 2px dashed var(--line); background: transparent; }
.zone-wrap.trash h3 { color: var(--muted); justify-content: center; margin: 0; }
.zone-wrap.trash .zone { min-height: 28px; }
.zone { min-height: 44px; display: flex; flex-direction: column; gap: 6px; }
.zone:empty::after { content: "Drop tiles here"; color: var(--muted); font-size: 13px; padding: 10px; text-align: center; opacity: .7; }
.zone[data-zone="trash"]:empty::after { content: ""; padding: 0; }
.zone[data-zone="pool"]:empty::after { content: "New tiles appear here"; }

/* Tiles */
.tile {
  background: var(--tile); border-radius: 8px; box-shadow: var(--shadow); padding: 8px 10px;
  cursor: grab; display: flex; gap: 8px; align-items: flex-start; border-left: 4px solid var(--line);
  touch-action: manipulation; user-select: none;
}
.zone[data-zone$="pro"] .tile { border-left-color: var(--pro); }
.zone[data-zone$="neu"] .tile { border-left-color: var(--neu); }
.zone[data-zone$="con"] .tile { border-left-color: var(--con); }
.zone[data-zone="pool"] .tile { border-left-color: var(--accent); }
.tile.breaker { box-shadow: 0 0 0 2px var(--con), var(--shadow); }
.zone[data-zone$="pro"] .tile.breaker { box-shadow: 0 0 0 2px var(--pro), var(--shadow); }
.tile-flag { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--con); }
.zone[data-zone$="pro"] .tile-flag { color: var(--pro); }
.tile-body { flex: 1; min-width: 0; }
.tile-text { word-wrap: break-word; }
.tile-note { font-size: 12px; color: var(--muted); margin-top: 2px; white-space: pre-wrap; word-wrap: break-word; }
.tile-dots { display: flex; gap: 2px; padding-top: 5px; flex-shrink: 0; }
.tile-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--line); }
.tile-dots i.on { background: var(--ink); opacity: .7; }
.tile-edit { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0 2px; font-size: 14px; line-height: 1; }
.tile-edit:hover { color: var(--accent); }
.sortable-ghost { opacity: .35; }
.sortable-chosen { cursor: grabbing; }
.sortable-drag { transform: rotate(2deg); }

/* Add form */
.add { display: flex; flex-direction: column; gap: 6px; }
.add textarea { width: 100%; resize: vertical; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--bg); }
.add-row { display: flex; align-items: center; gap: 8px; }
.hint { font-size: 12px; color: var(--muted); }

.suggest summary { cursor: pointer; font-size: 13px; color: var(--muted); padding: 4px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 6px; }
.chip { font-size: 12px; border: 1px solid var(--line); border-radius: 99px; padding: 3px 9px; background: var(--panel); cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* Editor dialog */
dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(460px, calc(100% - 32px));
  background: var(--panel); color: var(--ink); box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
#editor-form { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
#editor-form h2 { margin: 0; font-size: 18px; }
#editor-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
#editor-form textarea, #editor-form select { font-weight: normal; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--bg); resize: vertical; }
.weights { display: flex; gap: 6px; }
.weights button { flex: 1; border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 6px 0; cursor: pointer; font-weight: 600; }
.weights button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.weights-caption { font-size: 11px; font-weight: normal; color: var(--muted); display: flex; justify-content: space-between; }
#editor-form label.check { flex-direction: row; align-items: flex-start; gap: 8px; font-weight: 600; }
#editor-form label.check input { margin-top: 3px; accent-color: var(--con); }
.ed-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* Tap-to-move sheet */
.sheet form { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.sheet-title { font-weight: 600; word-wrap: break-word; }
.sheet-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sheet-col { display: flex; flex-direction: column; gap: 6px; }
.sheet-head { font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--muted); text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mv { border: 0; border-radius: 10px; padding: 14px 0; font-weight: 700; cursor: pointer; }
.mv.pro { background: var(--pro-bg); color: var(--pro); }
.mv.neu { background: var(--neu-bg); color: var(--neu); }
.mv.con { background: var(--con-bg); color: var(--con); }
.mv.here { outline: 2px solid currentColor; }
.sheet-row { display: flex; gap: 6px; }
.sheet-row .btn { flex: 1; padding: 10px; }
.spacer { flex: 1; }

/* Responsive */
@media (max-width: 960px) {
  .grid { grid-template-columns: 1fr 1fr; }
  .middle { grid-column: 1 / -1; grid-row: 1; position: static; max-height: none; }
}
@media (max-width: 640px) {
  .grid { grid-template-columns: 1fr; padding: 12px 16px; }
  .topbar { padding: 10px 16px; }
  .boards { order: 3; flex-basis: 100%; }
  .boards select { flex: 1; max-width: none; }
  .verdict { padding: 12px 16px 0; }
  .verdict-inner { grid-template-columns: 1fr; }
  .vbar { display: none; }
  .gut { padding: 10px 16px 0; }
  .gut-row { grid-template-columns: 1fr 1fr; }
  .gut-msg { grid-column: 1 / -1; grid-row: 2; }
  .facts { margin: 10px 16px 0; }
}
