:root {
  color-scheme: light;
  --page: #f9f9f7; --surface: #fcfcfb; --ink: #0b0b0b; --ink-2: #52514e;
  --muted: #898781; --grid: #e1e0d9; --baseline: #c3c2b7;
  --border: rgba(11,11,11,0.10);
  --gain: #006300; --loss: #d03b3b;
  --gain-wash: rgba(12,163,12,0.08); --loss-wash: rgba(208,59,59,0.08);
  --accent: #2a78d6; --accent-wash: rgba(42,120,214,0.10);
  --warn: #b7791f; --warn-wash: rgba(201,161,59,0.14);
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --ink: #ffffff; --ink-2: #c3c2b7;
    --muted: #898781; --grid: #2c2c2a; --baseline: #383835;
    --border: rgba(255,255,255,0.10);
    --gain: #0ca30c; --loss: #e66767;
    --gain-wash: rgba(12,163,12,0.12); --loss-wash: rgba(230,103,103,0.12);
    --accent: #3987e5; --accent-wash: rgba(57,135,229,0.16);
    --warn: #d9a441; --warn-wash: rgba(201,161,59,0.18);
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--page); color: var(--ink);
  font-family: system-ui, "Segoe UI", "Leelawadee UI", Tahoma, sans-serif;
  font-size: 15px; line-height: 1.55;
}
.wrap { max-width: 1100px; margin: 0 auto; padding: 20px 20px 64px; }

/* ---- nav ---- */
.nav { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.nav a {
  text-decoration: none; padding: 8px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--ink-2); font-weight: 600; font-size: 14px;
}
.nav a.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.nav .status { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.live { background: var(--gain); }
.dot.err { background: var(--loss); }
button.ghost {
  background: none; border: 1px solid var(--border); border-radius: 8px;
  color: var(--ink-2); padding: 5px 12px; font: inherit; font-size: 13px; cursor: pointer;
}
button.ghost:hover { background: var(--surface); border-color: var(--baseline); }

h1 { font-size: 20px; font-weight: 650; margin: 0 0 4px; }
.sub { color: var(--ink-2); font-size: 13px; margin: 0 0 16px; }

/* ---- tiles / cards ---- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.tile .label { font-size: 13px; color: var(--ink-2); }
.tile .value { font-size: 25px; font-weight: 650; margin-top: 2px; }
.tile.hero .value { font-size: 36px; }
.tile .delta { font-size: 13.5px; color: var(--ink-2); margin-top: 2px; }
.pl-pos { color: var(--gain); }
.pl-neg { color: var(--loss); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.card h2 { font-size: 15px; font-weight: 650; margin: 0 0 12px; }
.empty { text-align: center; color: var(--muted); padding: 30px 12px; font-size: 14px; }
.footnote { font-size: 12px; color: var(--muted); margin-top: 8px; }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 860px) { .cols2 { grid-template-columns: 1fr; } }

/* ---- form ---- */
form.add { display: grid; grid-template-columns: 1.4fr 1fr 1fr 0.7fr auto; gap: 10px; align-items: start; }
@media (max-width: 760px) { form.add { grid-template-columns: 1fr 1fr; } }
.field label { display: block; font-size: 12.5px; color: var(--ink-2); margin-bottom: 4px; }
.field input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--baseline); border-radius: 8px;
  background: var(--page); color: var(--ink); font: inherit;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.field .hint { font-size: 11.5px; color: var(--muted); margin-top: 3px; }
form.add button.primary { margin-top: 22px; }
button.primary {
  background: var(--accent); color: #fff; border: none; border-radius: 8px;
  padding: 9px 18px; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
button.primary:hover { filter: brightness(1.08); }
.form-msg { font-size: 13px; margin-top: 8px; color: var(--loss); min-height: 1em; }
.selected-pair { font-size: 12px; color: var(--gain); margin-top: 3px; min-height: 1em; }

/* ---- combobox ---- */
.combo { position: relative; }
.combo-list {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--surface); border: 1px solid var(--baseline); border-radius: 10px;
  max-height: 280px; overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,0.14);
  display: none;
}
.combo-list.open { display: block; }
.combo-item {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  padding: 8px 12px; cursor: pointer; font-size: 14px;
}
.combo-item:hover, .combo-item.active { background: var(--accent-wash); }
.combo-item .base { font-weight: 600; }
.combo-item .quote { color: var(--muted); font-size: 12.5px; }
.combo-item .badge {
  font-size: 11px; color: var(--accent); border: 1px solid var(--accent);
  border-radius: 999px; padding: 0 7px; margin-left: 6px;
}
.combo-note { padding: 10px 12px; font-size: 13px; color: var(--muted); }

/* ---- table ---- */
.table-scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: right; padding: 8px 9px; white-space: nowrap; font-size: 14px; }
th:first-child, td:first-child { text-align: left; }
thead th {
  font-size: 12.5px; font-weight: 600; color: var(--muted);
  border-bottom: 1px solid var(--baseline); background: var(--surface);
}
tbody td { border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: none; }
td.coin { font-weight: 600; }
td.coin .pair { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 4px; }
td .sub-num { display: block; font-size: 11.5px; color: var(--muted); font-weight: 400; }
tr.gain-row td.pl { background: var(--gain-wash); }
tr.loss-row td.pl { background: var(--loss-wash); }
td button.del {
  background: none; border: none; cursor: pointer; color: var(--muted);
  font-size: 15px; padding: 2px 6px; border-radius: 6px;
}
td button.del:hover { color: var(--loss); background: var(--loss-wash); }
tfoot td { font-weight: 650; border-top: 1px solid var(--baseline); font-variant-numeric: tabular-nums; }
td.price-cell.tick-up, .wr-price.tick-up { animation: flashUp 0.8s ease-out; }
td.price-cell.tick-down, .wr-price.tick-down { animation: flashDown 0.8s ease-out; }
@keyframes flashUp { from { background: var(--gain-wash); } to { background: transparent; } }
@keyframes flashDown { from { background: var(--loss-wash); } to { background: transparent; } }

/* ---- price board ---- */
.layout { display: grid; grid-template-columns: 320px minmax(0, 1fr); gap: 20px; align-items: start; }
.board { position: sticky; top: 16px; }
@media (max-width: 900px) { .layout { grid-template-columns: 1fr; } .board { position: static; } }
.board-head { display: flex; gap: 6px; align-items: center; margin-bottom: 10px; }
.board-head select {
  flex: 1; min-width: 0; padding: 7px 8px; border: 1px solid var(--baseline);
  border-radius: 8px; background: var(--page); color: var(--ink); font: inherit; font-size: 14px;
}
.icon-btn {
  background: none; border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
  color: var(--ink-2); padding: 5px 8px; font-size: 13px; line-height: 1;
}
.icon-btn:hover { border-color: var(--baseline); background: var(--page); }
.board-search { position: relative; margin-bottom: 8px; }
.board-search input {
  width: 100%; padding: 7px 10px; border: 1px solid var(--baseline); border-radius: 8px;
  background: var(--page); color: var(--ink); font: inherit; font-size: 13.5px;
}
.board-search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.watch-row {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px; padding: 7px 2px; border-bottom: 1px solid var(--grid);
}
.watch-row:last-child { border-bottom: none; }
.wr-name { font-weight: 600; font-size: 14px; white-space: nowrap; }
.wr-name .q { color: var(--muted); font-weight: 400; font-size: 11.5px; margin-left: 3px; }
.wr-right { text-align: right; margin-left: auto; }
.wr-price { font-variant-numeric: tabular-nums; font-size: 14px; display: block; border-radius: 4px; padding: 0 3px; }
.wr-chg { font-size: 11.5px; display: block; }
.watch-row .rm {
  background: none; border: none; cursor: pointer; color: var(--muted);
  font-size: 13px; padding: 2px 4px; border-radius: 6px; visibility: hidden;
}
.watch-row:hover .rm { visibility: visible; }
.watch-row .rm:hover { color: var(--loss); background: var(--loss-wash); }
.board-empty { color: var(--muted); font-size: 13px; text-align: center; padding: 16px 0; }

/* ---- bot page ---- */
.pill { font-size: 12px; border-radius: 999px; padding: 2px 10px; font-weight: 600; white-space: nowrap; }
.pill.hold { background: var(--gain-wash); color: var(--gain); }
.pill.cash { background: var(--accent-wash); color: var(--accent); }
.pill.stop { background: var(--warn-wash); color: var(--warn); }
.bot-coin { display: flex; align-items: center; gap: 12px; padding: 11px 4px; border-bottom: 1px solid var(--grid); }
.bot-coin:last-child { border-bottom: none; }
.bot-coin .sym { font-weight: 700; width: 48px; }
.bot-coin .mid { flex: 1; font-size: 12.5px; color: var(--muted); }
.bot-coin .right { text-align: right; font-variant-numeric: tabular-nums; }
.bot-coin .price { font-weight: 600; }
.bot-coin .pl { display: block; font-size: 12.5px; }
.bot-ev { padding: 7px 4px; border-bottom: 1px solid var(--grid); font-size: 13px; }
.bot-ev:last-child { border-bottom: none; }
.bot-ev .when { color: var(--muted); font-size: 11.5px; }
.bot-note { font-size: 12.5px; color: var(--muted); margin-top: 10px; }
.gauge { margin: 4px 0 10px; }
.gauge .g-label { display: flex; justify-content: space-between; font-size: 12px; color: var(--ink-2); margin-bottom: 3px; }
.gauge .g-track { height: 8px; border-radius: 999px; background: var(--grid); overflow: hidden; }
.gauge .g-fill { height: 100%; border-radius: 999px; background: var(--accent); transition: width .5s; }
.gauge .g-fill.warn { background: var(--warn); }
#botChart svg, .chartbox svg { display: block; width: 100%; }

/* ---- league page ---- */
.league-row { display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--grid); cursor: pointer; }
.league-row:hover { background: var(--accent-wash); border-radius: 8px; }
.league-row .medal { width: 28px; font-size: 17px; text-align: center; }
.league-row .lname { flex: 1; font-weight: 600; font-size: 14px; }
.league-row .lname .real-tag { font-size: 10.5px; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 0 6px; margin-left: 6px; }
.league-row .lval { text-align: right; font-variant-numeric: tabular-nums; font-size: 14px; }
.league-row .lval .lpl { display: block; font-size: 12px; }
.league-detail { display: none; padding: 6px 10px 12px 42px; border-bottom: 1px solid var(--grid); font-size: 12.5px; }
.league-detail.open { display: block; }
.league-detail .ld-coin { display: flex; justify-content: space-between; padding: 3px 0; color: var(--ink-2); }
.league-detail .ld-coin b { color: var(--ink); }
.league-detail svg { display: block; width: 100%; margin-top: 6px; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12.5px; color: var(--ink-2); margin-top: 8px; }
.legend .key { display: inline-block; width: 14px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 5px; }

/* ---- PIN lock ---- */
.lock-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: var(--page);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.lock-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 32px 28px; width: 100%; max-width: 360px; text-align: center;
}
.lock-card .lock-icon { font-size: 40px; margin-bottom: 8px; }
.lock-card h2 { font-size: 18px; margin: 0 0 4px; }
.lock-card p { font-size: 13.5px; color: var(--ink-2); margin: 0 0 18px; }
.lock-card input[type="password"] {
  width: 100%; padding: 12px; margin-bottom: 10px;
  border: 1px solid var(--baseline); border-radius: 10px;
  background: var(--page); color: var(--ink); font: inherit;
  font-size: 22px; text-align: center; letter-spacing: 8px;
}
.lock-card input[type="password"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.lock-card .remember {
  display: flex; align-items: center; justify-content: center; gap: 7px;
  font-size: 13px; color: var(--ink-2); margin: 4px 0 14px; cursor: pointer;
}
.lock-card button.primary { width: 100%; padding: 12px; font-size: 15px; }
.lock-err { color: var(--loss); font-size: 13px; min-height: 1.2em; margin-top: 10px; }

/* ---- ล็อกอิน (ช่องชื่อผู้ใช้/รหัสผ่าน) ---- */
.lock-card input[type="text"], .lock-card input[type="password"] {
  width: 100%; padding: 11px 12px; margin-bottom: 10px;
  border: 1px solid var(--baseline); border-radius: 10px;
  background: var(--page); color: var(--ink); font: inherit;
  font-size: 15px; text-align: left; letter-spacing: normal;
}
.lock-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.lock-card button:disabled { opacity: .6; cursor: wait; }
.linkbtn {
  background: none; border: none; color: var(--accent); font: inherit; font-size: 13px;
  cursor: pointer; margin-top: 12px; padding: 4px 8px; text-decoration: underline;
}
#whoami { font-size: 12.5px; color: var(--ink-2); }
.sync { font-size: 12px; color: var(--muted); white-space: nowrap; }
.sync.busy { color: var(--warn); }
.sync.err { color: var(--loss); }
.sync.ok { color: var(--gain); }

/* ---- ปุ่มขาย + โมดัลบันทึกการขาย ---- */
td.row-actions { white-space: nowrap; }
td button.sellbtn {
  background: none; border: 1px solid var(--gain); cursor: pointer; color: var(--gain);
  font: inherit; font-size: 12.5px; font-weight: 600;
  padding: 3px 12px; border-radius: 999px; margin-right: 4px;
}
td button.sellbtn:hover { background: var(--gain-wash); }
.sell-overlay {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.sell-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 24px; width: 100%; max-width: 420px; max-height: 92vh; overflow-y: auto;
}
.sell-card h2 { font-size: 16px; font-weight: 650; margin: 0 0 2px; }
.sell-card .sub { display: block; margin-bottom: 16px; }
.sell-card .field { margin-bottom: 12px; }
.sell-card textarea {
  width: 100%; min-height: 140px; padding: 8px 10px; border: 1px solid var(--baseline); border-radius: 8px;
  background: var(--page); color: var(--ink); font: 12px/1.4 ui-monospace, Consolas, monospace;
}
.sell-preview {
  font-size: 14px; padding: 10px 12px; border-radius: 8px;
  background: var(--page); border: 1px dashed var(--baseline); min-height: 1.4em;
}
.sell-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.card-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.card-head h2 { margin-right: auto; }
