/* Phone is the base layout; min-width queries add the wider arrangements. */

: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;
  --on-neg: #ffffff;
  --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;
  --gutter: 12px;
  --tap: 44px;
  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;
    --on-neg: #1c0b0c;
    --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);
  -webkit-tap-highlight-color: transparent;
}

::selection { background: var(--accent-soft); color: var(--text); }

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

.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* Buttons */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  min-height: var(--tap);
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
  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.danger.armed { background: var(--neg); border-color: var(--neg); color: var(--on-neg); }

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

/* Form controls. 16px text stops iOS from zooming the page on focus. */
.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"] {
  width: 100%;
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  min-height: var(--tap);
}
input[type="number"] { font-family: var(--mono); font-variant-numeric: tabular-nums; }
input:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
input[aria-invalid="true"] { border-color: var(--neg); }
.input-unit { display: flex; align-items: center; gap: 8px; }
.input-unit input { max-width: 180px; }
.input-unit .ccy { font-size: 13px; color: var(--text-3); }

.help { margin: 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 20px; box-shadow: var(--shadow);
}
.login h1 { font-size: 20px; margin-bottom: 20px; }
.login form { display: grid; gap: 16px; }

/* Shell */
.topbar {
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  max-width: 1280px; margin: 0 auto; padding: 6px var(--gutter);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.brand { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 10px; min-width: 0; }
.brand-name { font-weight: 600; width: 100%; }
.feed { font-size: 12px; color: var(--text-3); }
.feed[data-state="live"] { color: var(--pos); }
.feed[data-state="partial"] { color: var(--warn-text); }
.feed[data-state="stale"], .feed[data-state="down"] { color: var(--neg); }

.layout {
  max-width: 1280px; margin: 0 auto;
  padding: var(--gutter) var(--gutter) calc(96px + env(safe-area-inset-bottom));
  display: grid; gap: var(--gutter);
}
.layout > * { min-width: 0; }

.banner { margin: 0; padding: 10px 12px; 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); }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; min-width: 0; }

/* Totals: three across on a phone, six across on desktop */
.stats {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.stat { padding: 10px; display: grid; gap: 1px; align-content: start; min-width: 0; border-left: 1px solid var(--border); }
.stat:nth-child(3n + 1) { border-left: 0; }
.stat:nth-child(n + 4) { border-top: 1px solid var(--border); }
.stat-label { font-size: 12px; color: var(--text-3); }
.stat-value { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-sub { font-size: 11px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.loading .stat-value { color: transparent; background: var(--surface-2); border-radius: 4px; width: 70%; }

/* Ticket */
.trade-row { display: grid; gap: var(--gutter); }
.ticket { display: grid; gap: 12px; }
.ticket-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.ticket-head h2 { margin: 0; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: var(--radius); padding: 3px; gap: 2px; }
.seg button {
  appearance: none; border: 0; background: transparent; color: var(--text-2); font: inherit; font-weight: 500;
  min-height: 38px; padding: 4px 12px; border-radius: 6px; cursor: pointer; touch-action: manipulation;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.seg button:hover { color: var(--text); }
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(23 25 26 / 0.12); }

.quote { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.side {
  appearance: none; font: inherit; cursor: pointer; touch-action: manipulation;
  border-radius: var(--radius); padding: 12px; min-height: 72px;
  display: grid; gap: 2px; align-content: center; text-align: left; border: 1px solid transparent;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.side.sell { background: var(--neg); color: var(--on-neg); }
.side.buy { background: var(--accent); color: var(--accent-text); text-align: right; }
.side:hover:not(:disabled) { filter: brightness(1.08); }
.side:active:not(:disabled) { transform: translateY(1px); }
.side:disabled { opacity: 0.55; cursor: not-allowed; }
.side-label { font-size: 13px; font-weight: 600; }
.side-price { font-size: 20px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ticket-help { margin-top: -4px; }
.close-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.result { border-top: 1px solid var(--border); padding-top: 12px; }
.result-title { margin: 0 0 8px; font-weight: 500; font-size: 14px; }
.result-list { display: grid; gap: 4px; font-size: 14px; }
.result-list li { display: flex; justify-content: space-between; gap: 12px; }
.res { min-width: 0; overflow-wrap: anywhere; text-align: right; }
.res[data-ok="true"] { color: var(--pos); }
.res[data-ok="false"] { color: var(--neg); }
.res[data-ok="pending"], .res[data-ok="null"], .result[data-state="busy"] .res { color: var(--text-3); }

/* Daily target */
.target { display: grid; gap: 10px; }
.target-head { display: grid; gap: 2px; }
.target-head h2 { margin: 0; }
.target-read { margin: 0; font-size: 14px; color: var(--text-2); }
.meter { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.meter-fill {
  display: block; height: 100%; background: var(--accent); border-radius: 4px;
  transform-origin: left; transform: scaleX(0); transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}
.meter[data-done="true"] .meter-fill { background: var(--pos); }

/* Accounts: a compact card per account on a phone */
.acct-head { display: none; }
.accts { display: grid; }
.acct {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px;
  padding: 12px 0; border-top: 1px solid var(--border);
}
.acct:first-child { border-top: 0; padding-top: 0; }
.acct-id { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; }
.acct-label { font-weight: 600; }
.acct-login { font-size: 12px; color: var(--text-3); width: 100%; }
.state { font-size: 13px; font-weight: 500; color: var(--text-3); }
.state[data-state="live"] { color: var(--pos); }
.state[data-state="down"] { color: var(--neg); }
.state[data-state="paused"] { color: var(--warn-text); }
.fig { display: grid; min-width: 0; }
.fig-label { font-size: 12px; color: var(--text-3); }
.fig-value { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.acct-lot { display: grid; gap: 2px; }
.acct-lot input { width: 100%; }

/* Trading switch: the real checkbox stays for keyboard and screen readers */
.switch { display: flex; align-items: center; gap: 10px; align-self: end; min-height: var(--tap); cursor: pointer; position: relative; }
.switch-input { position: absolute; opacity: 0; width: 44px; height: 26px; margin: 0; cursor: pointer; }
.switch-track {
  width: 44px; height: 26px; border-radius: 13px; background: var(--border); position: relative; flex-shrink: 0;
  transition: background-color 0.18s ease;
}
.switch-track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface); box-shadow: 0 1px 2px rgb(23 25 26 / 0.25); transition: transform 0.18s ease;
}
.switch-input:checked + .switch-track { background: var(--accent); }
.switch-input:checked + .switch-track::after { transform: translateX(18px); }
.switch-input:focus-visible + .switch-track { outline: 2px solid var(--focus); outline-offset: 2px; }
.switch-text { font-size: 14px; color: var(--text-2); }

/* Signals */
.sigs { display: grid; }
.sig { padding: 10px 0; border-top: 1px solid var(--border); display: grid; gap: 4px; }
.sig:first-child { border-top: 0; padding-top: 0; }
.sig-meta { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--text-3); }
.sig-text { margin: 0; font-weight: 500; overflow-wrap: anywhere; }
.sig-results { display: grid; gap: 2px; font-size: 13px; }
.sig-results li { display: flex; justify-content: space-between; gap: 12px; }
.sig-acct { color: var(--text-2); flex-shrink: 0; }

/* Save bar sits above the home indicator */
.savebar {
  position: fixed; left: var(--gutter); right: var(--gutter); bottom: calc(var(--gutter) + env(safe-area-inset-bottom)); z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--surface); border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 8px 8px 8px 14px; box-shadow: 0 4px 16px rgb(23 25 26 / 0.18);
}
.savebar-msg { margin: 0; font-size: 14px; color: var(--text-2); min-width: 0; }
.savebar-msg[data-tone="bad"] { color: var(--neg); }
.savebar-actions { display: flex; gap: 6px; flex-shrink: 0; }

.toast {
  position: fixed; left: var(--gutter); right: var(--gutter); top: calc(64px + env(safe-area-inset-top)); z-index: 30;
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: var(--radius); font-size: 14px;
  pointer-events: none;
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; border: 2px solid var(--bg); }
::-webkit-scrollbar-track { background: transparent; }

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

/* Larger phones and tablets */
@media (min-width: 640px) {
  :root { --gutter: 16px; }
  .layout { gap: 16px; }
  .brand { flex-wrap: nowrap; gap: 12px; }
  .brand-name { width: auto; }
  .feed { font-size: 13px; }
  .topbar-inner { padding: 10px var(--gutter); }
  .panel { padding: 16px; }
  .stat { padding: 12px 14px; }
  .stat-value { font-size: 18px; }
  .stat-label { font-size: 13px; }
  .acct { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .target { grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: 10px 24px; }
  .target-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
  .meter { grid-column: 1; }
  .target-edit { grid-column: 2; grid-row: 1 / span 2; }
  .savebar { left: 50%; right: auto; transform: translateX(-50%); width: min(560px, calc(100% - 32px)); }
  .toast { left: auto; width: 360px; }
}

/* Desktop */
@media (min-width: 1000px) {
  .layout { gap: 20px; padding-top: 20px; }
  .stats { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .stat { padding: 14px 16px; }
  .stat:nth-child(n) { border-left: 1px solid var(--border); border-top: 0; }
  .stat:first-child { border-left: 0; }
  .stat-value { font-size: 20px; }
  .stat-sub { font-size: 12px; }

  .trade-row { grid-template-columns: minmax(360px, 1fr) minmax(0, 1.4fr); gap: 20px; align-items: stretch; }
  .target { grid-template-columns: 1fr; align-content: start; }
  .target-edit { grid-column: 1; grid-row: auto; }
  .side-price { font-size: 22px; }

  /* Accounts become one comparable row each */
  .acct-head, .acct {
    display: grid; align-items: center; gap: 12px;
    grid-template-columns: minmax(180px, 1.6fr) repeat(4, minmax(0, 1fr)) 110px 120px;
  }
  .acct-head { font-size: 13px; color: var(--text-3); padding-bottom: 8px; }
  .acct-head .r, .acct .fig { text-align: right; }
  .acct { padding: 10px 0; }
  .acct:first-child { border-top: 1px solid var(--border); padding-top: 10px; }
  .acct-id { grid-column: auto; }
  .fig-label { display: none; }
  .acct-lot .fig-label { display: none; }
  .switch { align-self: center; }

  .sig {
    grid-template-columns: 150px minmax(0, 1fr) minmax(0, 380px); align-items: start; gap: 16px; padding: 10px 0;
  }
  .sig-meta { display: grid; gap: 0; justify-content: start; }
  .sig-results { gap: 0; }
}
