:root {
  --bg: #f4f5f4;
  --surface: #ffffff;
  --surface-2: #eceeed;
  --border: #d9ddda;
  --text: #17191a;
  --text-2: #50585a;
  --text-3: #6a7275;
  --accent: #0d7a55;
  --accent-text: #ffffff;
  --accent-soft: #e2f2ea;
  --pos: #0d7a55;
  --neg: #b4282d;
  --neg-soft: #fbe9e9;
  --warn-soft: #fbf1dc;
  --warn-text: #7a5200;
  --focus: #0d7a55;
  --radius: 8px;
  --shadow: 0 1px 2px rgb(23 25 26 / 0.06);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111314;
    --surface: #181b1c;
    --surface-2: #202425;
    --border: #2d3233;
    --text: #e8ebea;
    --text-2: #a9b1b0;
    --text-3: #8d9594;
    --accent: #3cc48d;
    --accent-text: #08130e;
    --accent-soft: #15301f;
    --pos: #3cc48d;
    --neg: #f07b7e;
    --neg-soft: #3a1d1e;
    --warn-soft: #362a12;
    --warn-text: #f0c46a;
    --focus: #3cc48d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

/* Component display rules must not beat the hidden attribute. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
}

h1, h2 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin-bottom: 12px; }

.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  padding: 7px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.btn.danger { color: var(--neg); }
.btn.small { padding: 3px 10px; font-size: 13px; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Form controls */
.field { display: grid; gap: 4px; }
.field label { font-size: 13px; font-weight: 500; color: var(--text-2); }
input[type="text"], input[type="password"], input[type="number"], input:not([type]), select {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 10px;
  min-height: 38px;
}
input[type="number"] { font-family: var(--mono); font-variant-numeric: tabular-nums; }
input:focus-visible, select:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
input[aria-invalid="true"] { border-color: var(--neg); }

.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }

.help { margin: 4px 0 0; font-size: 13px; color: var(--text-3); }
.error { margin: 6px 0 0; font-size: 13px; color: var(--neg); }
.empty { margin: 0; color: var(--text-3); font-size: 14px; }

/* Login */
.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: var(--shadow);
}
.login h1 { font-size: 20px; margin-bottom: 20px; }
.login form { display: grid; gap: 16px; }

/* App shell */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner {
  max-width: 1280px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { display: flex; align-items: baseline; gap: 12px; min-width: 0; flex-wrap: wrap; }
.brand-name { font-weight: 600; }
.feed { font-size: 13px; color: var(--text-3); }
.feed[data-state="live"] { color: var(--pos); }
.feed[data-state="stale"], .feed[data-state="down"] { color: var(--neg); }
.topbar-actions { display: flex; gap: 6px; }
#toggle-trading[data-running="false"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.layout { max-width: 1280px; margin: 0 auto; padding: 20px 16px 120px; }

.banner {
  margin: 0 0 16px; padding: 10px 14px; border-radius: var(--radius);
  background: var(--warn-soft); color: var(--warn-text); font-size: 14px;
}
.banner[data-tone="bad"] { background: var(--neg-soft); color: var(--neg); }

.stats {
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 20px;
}
.stat { padding: 14px 16px; display: grid; gap: 2px; border-left: 1px solid var(--border); min-width: 0; }
.stat:first-child { border-left: 0; }
.stat-label { font-size: 13px; color: var(--text-3); }
.stat-value { font-size: 20px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.columns { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 20px; align-items: start; }
.col-main, .col-side { display: grid; gap: 20px; min-width: 0; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; min-width: 0;
}
.panel + .panel h2 { margin-top: 0; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 500; color: var(--text-3); font-size: 13px; padding: 0 8px 8px; white-space: nowrap; }
td { padding: 7px 8px; border-top: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { padding-left: 0; }
th:last-child, td:last-child { padding-right: 0; }
th.r, td.r { text-align: right; }
td.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
td.reason { white-space: normal; color: var(--text-2); min-width: 160px; }
table:has(tbody:empty) { display: none; }

.signals { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 14px; }
.signals li { display: grid; grid-template-columns: 140px minmax(0, 1fr); gap: 12px; }
.signals .sym { font-weight: 500; }
.signals .why { color: var(--text-2); }

/* Settings */
.symbols { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; }

.group + .group { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }
.group-fields { display: grid; gap: 14px; }
.input-unit { display: flex; align-items: center; gap: 8px; }
.input-unit input { max-width: 140px; }
.input-unit span { font-size: 13px; color: var(--text-3); }

.chats { list-style: none; margin: 12px 0; padding: 0; display: grid; gap: 6px; }
.chats li {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 10px; background: var(--surface-2); border-radius: var(--radius);
}
.chat-meta { min-width: 0; display: grid; }
.chat-label { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-id { font-family: var(--mono); font-size: 13px; color: var(--text-2); }
.chat-actions { display: flex; gap: 4px; flex-shrink: 0; }
.add-chat { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; margin-top: 12px; }

.savebar {
  position: sticky; bottom: 16px; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--surface); border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 10px 12px 10px 16px; box-shadow: 0 2px 6px rgb(23 25 26 / 0.1);
}
.savebar-msg { margin: 0; font-size: 14px; color: var(--text-2); }
.savebar-msg[data-tone="bad"] { color: var(--neg); }
.savebar-actions { display: flex; gap: 6px; }

.toast {
  position: fixed; right: 16px; top: 64px; z-index: 30; max-width: 360px;
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: var(--radius); font-size: 14px;
}

/* Loading skeleton for stats before the first status arrives */
.loading .stat-value { color: transparent; background: var(--surface-2); border-radius: 4px; width: 70%; }

@media (pointer: coarse) {
  .btn, .btn.small { min-height: 44px; }
  .check { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

@media (max-width: 1024px) {
  .columns { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat { border-left: 0; border-top: 1px solid var(--border); }
  .stat:nth-child(-n+2) { border-top: 0; }
  .stat:nth-child(even) { border-left: 1px solid var(--border); }
  .stat:last-child { grid-column: 1 / -1; border-left: 0; }
  .add-chat { grid-template-columns: 1fr 1fr; }
  .add-chat .btn { grid-column: 1 / -1; }
  .signals li { grid-template-columns: 1fr; gap: 0; }
  .brand-name { font-size: 14px; }
  .topbar-actions .btn { padding: 6px 10px; }
}
