/* TEST — phone-first. Light by default (readable outdoors), dark follows the system. */
:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --ink: #16202c;
  --muted: #5f6e80;
  --grid: #e6eaf0;
  --line: #d5dbe3;
  --up: #0e9f6e;
  --down: #d6453d;
  --live: #c77700;
  --accent: #1d4ed8;
  --accent-ink: #ffffff;
  --btn-ce: #15845a;
  --btn-pe: #c23f38;

  --font: "Barlow", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-num: "Barlow Semi Condensed", "Barlow", system-ui, -apple-system, sans-serif;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c141e;
    --surface: #111b27;
    --ink: #e4eaf1;
    --muted: #8193a8;
    --grid: #1a2735;
    --line: #243345;
    --up: #2fc08a;
    --down: #f0615a;
    --live: #f2b441;
    --accent: #7aa2ff;
    --accent-ink: #0c141e;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
           env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

button { font: inherit; color: inherit; }

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

/* ---------------- Login ---------------- */

.login {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  max-width: 460px;
  margin: 0 auto;
  padding: 12vh 24px 32px;
}

.wordmark {
  font-family: var(--font-num);
  font-weight: 600;
  font-size: clamp(88px, 30vw, 148px);
  line-height: 0.85;
  letter-spacing: -0.02em;
  margin: 0;
}

.wordmark-sub {
  margin: 20px 0 0;
  font-size: 20px;
  color: var(--muted);
  max-width: 22ch;
}

.login-actions { display: grid; gap: 14px; }

.btn-primary {
  display: block;
  width: 100%;
  padding: 16px 20px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
}
.btn-primary:active { transform: translateY(1px); }

.login-note {
  margin: 0;
  font-size: 14px;
  color: var(--muted);
}

.login-error {
  margin: 0;
  padding: 12px 14px;
  border-left: 3px solid var(--down);
  background: var(--surface);
  font-size: 15px;
}
.login-error[hidden] { display: none; }

/* ---------------- App ---------------- */

.app {
  max-width: 1100px;
  margin: 0 auto;
}

/* First screen: exactly one viewport tall. Controls on top, chart takes the rest. */
.screen {
  display: flex;
  flex-direction: column;
  height: calc(100vh - env(safe-area-inset-top, 0px));
  height: calc(100dvh - env(safe-area-inset-top, 0px));
  min-height: 480px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 6px;
}
.symbol { margin: 0; font-size: 15px; font-weight: 600; }
.topbar-expiry { margin-left: auto; font-size: 13px; color: var(--muted); }

.status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 8px 1px 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 12px;
  color: var(--muted);
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.status[data-state="live"] { color: var(--live); border-color: currentColor; }
.status[data-state="live"] .status-dot { background: var(--live); animation: pulse 1.6s ease-in-out infinite; }
.status[data-state="reconnecting"] .status-dot { background: var(--down); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

/* ---- Trade bar ---- */
.trade-bar { padding: 0 12px 8px; }

.buy-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.buy-tile {
  display: grid;
  gap: 2px;
  min-height: 58px;
  padding: 8px 12px;
  border: 0;
  border-radius: 12px;
  color: #fff;
  text-align: left;
}
.buy-ce { background: var(--btn-ce); }
.buy-pe { background: var(--btn-pe); }
.bt-top, .bt-sub {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 6px;
}
.bt-label { font-size: 15px; font-weight: 600; }
.bt-price {
  font-family: var(--font-num);
  font-weight: 600;
  font-size: 22px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.bt-sub {
  font-size: 12.5px;
  opacity: 0.9;
  font-variant-numeric: tabular-nums;
}

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}
.act {
  min-height: 40px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  border: 1.5px solid transparent;
}
.act-both { background: var(--accent); color: var(--accent-ink); }
.act-exit { background: transparent; border-color: var(--ink); color: var(--ink); }

.buy-tile:disabled, .act:disabled { opacity: 0.4; }
.buy-tile:not(:disabled):active, .act:not(:disabled):active { transform: translateY(1px); }

/* ---- OHLC ---- */
.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 2px 8px;
  padding: 6px 12px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.legend b { color: var(--ink); font-weight: 500; }
.legend-time { order: -1; flex-basis: 100%; font-size: 12px; }
.legend-sig { margin-left: 10px; color: var(--ink); font-weight: 500; }

@media (min-width: 520px) {
  .legend { justify-content: flex-start; gap: 4px 16px; }
  .legend-time { order: 0; flex-basis: auto; margin-left: auto; font-size: 13px; }
}

/* ---- Chart ---- */
.chart-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 220px;
  background: var(--surface);
}
#chart { position: absolute; inset: 0; }

.chart-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  z-index: 2;
  background: var(--surface);
}
.chart-message[hidden] { display: none; }
.chart-message p { max-width: 34ch; margin: 0 0 14px; }
.chart-message button {
  padding: 10px 18px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}

/* Order messages float over the top of the chart, so nothing jumps around */
.toast {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  z-index: 3;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  max-width: 560px;
  margin: 0 auto;
  padding: 10px 8px 10px 12px;
  border-radius: 10px;
  border-left: 4px solid var(--muted);
  background: var(--ink);
  color: var(--surface);
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.toast[hidden] { display: none; }
.toast[data-tone="good"] { border-left-color: var(--up); }
.toast[data-tone="bad"] { border-left-color: var(--down); }
.toast span { flex: 1; }
.toast-close {
  border: 0;
  background: none;
  color: inherit;
  font-size: 20px;
  line-height: 1;
  padding: 0 6px;
  opacity: 0.7;
}

/* ---------------- Below the fold ---------------- */
.panel { padding: 14px 16px; }
.panel + .panel { border-top: 1px solid var(--line); }

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.panel-title { margin: 0; font-size: 16px; font-weight: 600; }

.link-btn {
  border: 0;
  background: none;
  padding: 6px 0;
  font-size: 14px;
  color: var(--accent);
}

.pos-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
}
.pos-summary:empty { display: none; }
.stat {
  display: grid;
  gap: 2px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--surface);
}
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value {
  font-family: var(--font-num);
  font-size: 18px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
[data-dir="up"] { color: var(--up); }
[data-dir="down"] { color: var(--down); }

.pos-list { margin-top: 6px; }
.pos-empty { margin: 8px 0 0; font-size: 14px; color: var(--muted); }
.pos-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}
.pos-main { display: grid; gap: 1px; min-width: 0; }
.pos-name { font-size: 15px; font-weight: 600; }
.pos-qty { font-size: 12px; color: var(--muted); }
.pos-px { display: grid; gap: 1px; font-size: 12px; color: var(--muted); text-align: right; }
.pos-pnl { min-width: 58px; font-size: 15px; font-weight: 600; text-align: right; }
.pos-exit {
  padding: 8px 10px;
  border: 1.5px solid var(--ink);
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
}
.pos-exit:disabled { opacity: 0.4; }
.pos-booked { margin: 10px 0 0; font-size: 13px; }
.pos-booked[hidden] { display: none; }

.tools { display: flex; align-items: center; gap: 8px; }
.tools-name { font-size: 13px; font-weight: 600; color: var(--muted); margin-right: 2px; }
.chip {
  padding: 5px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--surface); }

.caption { margin: 10px 0 0; font-size: 12px; color: var(--muted); }
.logout { margin-top: 10px; color: var(--muted); }

/* ---------------- Confirm sheet ---------------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(8, 14, 22, 0.5);
}
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 480px;
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom, 0px));
  border-radius: 18px 18px 0 0;
  background: var(--surface);
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.25);
}
.sheet-title { margin: 0 0 10px; font-size: 20px; }
.sheet-lines { margin: 0; padding-left: 18px; font-size: 15px; }
.sheet-lines li + li { margin-top: 6px; }
.sheet-note { margin: 12px 0 0; font-size: 13px; color: var(--muted); }
.sheet-actions {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 10px;
  margin-top: 18px;
}
.sheet-cancel {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: transparent;
  font-size: 17px;
}
.sheet-ok { font-size: 17px; }

@media (min-width: 760px) {
  .topbar, .trade-bar, .legend { padding-left: 20px; padding-right: 20px; }
  .panel { padding-left: 20px; padding-right: 20px; }
  .chart-wrap { border-radius: 12px; margin: 0 12px 12px; overflow: hidden; }
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .status[data-state="live"] .status-dot { animation: none; }
}
