/* ============ Màu sắc (sáng / tối) ============ */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #dfe2e7;
  --text: #121620;
  --muted: #5f6878;
  --accent: #c99400;
  --accent-text: #1a1400;
  --up: #0a9c64;
  --down: #d63b52;
  --red-bg: #fde8eb;
  --red-text: #a31d33;
  --orange-bg: #fff2dc;
  --orange-text: #8a5300;
  --info-bg: #e7f0fd;
  --info-text: #1d4f91;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  --tabbar-h: 62px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0f17;
    --surface: #141a24;
    --surface-2: #1c2431;
    --border: #273042;
    --text: #e8ebf1;
    --muted: #8d97a8;
    --accent: #f0b90b;
    --accent-text: #1a1400;
    --up: #0ecb81;
    --down: #f6465d;
    --red-bg: #3a1620;
    --red-text: #ff9aa9;
    --orange-bg: #3a2a0e;
    --orange-text: #ffc66b;
    --info-bg: #13253f;
    --info-text: #9cc4ff;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0b0f17;
  --surface: #141a24;
  --surface-2: #1c2431;
  --border: #273042;
  --text: #e8ebf1;
  --muted: #8d97a8;
  --accent: #f0b90b;
  --accent-text: #1a1400;
  --up: #0ecb81;
  --down: #f6465d;
  --red-bg: #3a1620;
  --red-text: #ff9aa9;
  --orange-bg: #3a2a0e;
  --orange-text: #ffc66b;
  --info-bg: #13253f;
  --info-text: #9cc4ff;
  --shadow: none;
  color-scheme: dark;
}

/* ============ Nền tảng ============ */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
  -webkit-tap-highlight-color: transparent;
}
h1, h2 { margin: 0; font-weight: 650; }
h2 { font-size: 17px; margin-bottom: 10px; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 14px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.hint { color: var(--muted); font-size: 13.5px; margin: 6px 2px; }
.hint:empty { display: none; }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 20px; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.conn.ok { background: var(--up); }
.conn.bad { background: var(--down); }

main { padding: 12px 16px; max-width: 1180px; margin: 0 auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card.tight { padding: 10px; }

/* ============ Form ============ */
.row { display: flex; gap: 10px; align-items: flex-end; }
.grow { flex: 1; min-width: 0; }
.field { display: block; font-size: 14px; color: var(--muted); margin-bottom: 12px; }
.field b { color: var(--text); }
input, select {
  display: block; width: 100%;
  margin-top: 5px;
  min-height: 46px;
  padding: 10px 12px;
  font-size: 17px; /* >=16px để iPhone không tự phóng to */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  -webkit-appearance: none; appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 21px, calc(100% - 12px) 21px;
  background-size: 6px 6px; background-repeat: no-repeat;
  padding-right: 30px;
}
input:focus, select:focus { border-color: var(--accent); }
input.invalid { border-color: var(--down); }
input[type="range"] { padding: 0; min-height: 36px; background: none; border: none; accent-color: var(--accent); -webkit-appearance: auto; appearance: auto; }

.input-btn { display: flex; gap: 8px; margin-top: 5px; }
.input-btn input { margin-top: 0; flex: 1; min-width: 0; }

.btn {
  min-height: 46px; padding: 0 16px;
  font: inherit; font-size: 15.5px; font-weight: 600;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text);
  cursor: pointer; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 14px; }
.btn.block { display: block; width: 100%; margin-top: 10px; }

.chips { display: flex; gap: 8px; margin: 2px 0 8px; }
.chips.wrap { flex-wrap: wrap; }
.scroll-x { overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.scroll-x::-webkit-scrollbar { display: none; }

.seg {
  display: flex; padding: 3px; gap: 3px; margin-bottom: 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 11px;
}
.seg button {
  flex: 1; min-height: 42px; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted);
  font: inherit; font-weight: 600; font-size: 15px; cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.seg.side button.long.on { background: var(--up); color: #fff; }
.seg.side button.short.on { background: var(--down); color: #fff; }
.seg button:disabled { opacity: .35; cursor: default; }
.seg.compact { margin-bottom: 12px; flex: 0 0 auto; }
.seg.compact button { padding: 0 12px; }

.toggle, .tf {
  min-height: 36px; padding: 0 13px; border-radius: 18px;
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.toggle.on, .tf.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.coin-chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 6px 6px 12px; border-radius: 18px;
  background: var(--surface-2); border: 1px solid var(--border); font-weight: 600; font-size: 15px;
}
.coin-chip button {
  width: 28px; height: 28px; border: 0; border-radius: 50%;
  background: transparent; color: var(--muted); font-size: 18px; cursor: pointer;
}

/* ============ Kết quả máy tính rủi ro ============ */
.result .big { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .lbl { font-size: 12.5px; color: var(--muted); }
.stat .val { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; word-break: break-all; }
.stat .sub { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stat.red .val { color: var(--down); }

.kv { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 7px 12px; font-size: 14.5px; }
.kv > :nth-child(odd) { color: var(--muted); }
.kv > :nth-child(even) { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.kv > .wrap { white-space: normal !important; overflow-wrap: anywhere; max-width: 62vw; }

table.tps { width: 100%; border-collapse: collapse; font-size: 14.5px; font-variant-numeric: tabular-nums; margin: 6px 0 4px; }
table.tps th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12.5px; padding: 4px 6px; }
table.tps td { padding: 9px 6px; border-top: 1px solid var(--border); }
table.tps tr.pick { cursor: pointer; }
table.tps tr.pick:active { background: var(--surface-2); }
table.tps td.num, table.tps th.num { text-align: right; }
.neg { color: var(--down); }
.pos { color: var(--up); }

.warn { border-radius: 10px; padding: 10px 12px; margin: 8px 0; font-size: 14.5px; }
.warn.do { background: var(--red-bg); color: var(--red-text); font-weight: 600; }
.warn.cam { background: var(--orange-bg); color: var(--orange-text); }
.warn.thong_tin { background: var(--info-bg); color: var(--info-text); }

.sec-title { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 14px 0 4px; }
.disclaimer { font-size: 12.5px; color: var(--muted); font-style: italic; margin: 10px 2px; }

/* ============ Biểu đồ ============ */
.chart-wrap { position: relative; padding: 4px; }
#chart { height: 58vh; min-height: 320px; }
.legend {
  position: absolute; left: 12px; top: 8px; z-index: 3; pointer-events: none;
  font-size: 12.5px; font-variant-numeric: tabular-nums; color: var(--muted);
}
.legend b { color: var(--text); }

/* ============ Nhật ký ============ */
.jrow { display: flex; justify-content: space-between; gap: 10px; }
.badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 6px; }
.badge.long { background: var(--up); color: #fff; }
.badge.short { background: var(--down); color: #fff; }

/* ============ Thanh tab dưới ============ */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11.5px; font-weight: 600;
}
.tabbar svg { width: 25px; height: 25px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.on { color: var(--accent); }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  max-width: calc(100% - 32px); z-index: 20;
  background: var(--text); color: var(--bg);
  padding: 11px 16px; border-radius: 12px; font-size: 14.5px;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }
#toast.err { background: var(--down); color: #fff; }

/* ============ Màn hình rộng (máy tính) ============ */
@media (min-width: 900px) {
  main { padding: 20px 24px; }
  .grid-2 { display: grid; grid-template-columns: minmax(360px, 440px) 1fr; gap: 16px; align-items: start; }
  #chart { height: 66vh; }
  .tabbar {
    top: 0; bottom: auto; left: 50%; right: auto; transform: translateX(-50%);
    width: min(640px, 100%); height: 56px; padding: 0;
    background: transparent; border: 0;
  }
  .tabbar a { flex-direction: row; gap: 7px; font-size: 14px; }
  .tabbar svg { width: 20px; height: 20px; }
  .topbar { padding: 14px 24px; }
  .topbar h1 { visibility: hidden; }
  body { padding-bottom: 24px; }
  #toast { bottom: 24px; }
}

/* ============ Bảng trượt (form) ============ */
#sheet-backdrop { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.45); }
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 31;
  max-height: 88vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface); border-radius: 18px 18px 0 0; border-top: 1px solid var(--border);
  padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.sheet-head h2 { margin: 0; }
.btn-row { display: flex; gap: 10px; margin-top: 12px; }
.btn-row .btn { flex: 1; }
.btn.danger { color: var(--down); border-color: var(--down); background: transparent; }

/* ============ Đăng nhập ============ */
#login {
  position: fixed; inset: 0; z-index: 40; background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
#login .card { width: min(380px, 100%); text-align: center; }
#login .field { text-align: left; }
#login img { border-radius: 14px; }

/* ============ Nhật ký ============ */
.jcard .jrow + .kv { margin-top: 8px; }
.jcard .btn-row .btn { min-height: 40px; font-size: 14px; padding: 0 8px; }
.pill { display: inline-block; font-size: 12.5px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.lock-banner { margin: 0 0 4px; }
.sum-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

@media (min-width: 900px) {
  #sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: 520px; border-radius: 18px; border: 1px solid var(--border); }
  #journal-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 0 12px; }
}
.sec-title.first { margin-top: 0; }

/* ============ Thống kê / backtest ============ */
details.more { border-top: 1px solid var(--border); padding: 8px 0 2px; margin-bottom: 8px; }
details.more > summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 15px; padding: 6px 0; list-style: none; }
details.more > summary::before { content: "▸ "; }
details.more[open] > summary::before { content: "▾ "; }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14.5px; margin: 8px 0; }
.check input { width: 22px; height: 22px; min-height: 0; margin: 0; flex: 0 0 auto; -webkit-appearance: auto; appearance: auto; accent-color: var(--accent); }

.cond-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-end; padding: 8px; margin-bottom: 8px;
  background: var(--surface-2); border-radius: 10px; }
.cond-row select, .cond-row input { min-height: 40px; font-size: 15px; padding: 6px 8px; background: var(--surface); }
.cond-row > select { flex: 1 1 100%; }
.cond-row .p { flex: 1 1 70px; font-size: 12px; color: var(--muted); }
.cond-row .p input, .cond-row .p select { margin-top: 2px; }
.cond-row .rm { flex: 0 0 auto; min-height: 40px; }
.subhead { font-weight: 650; margin: 12px 0 6px; }

.verdict { border-radius: 12px; padding: 12px 14px; font-weight: 650; margin-bottom: 10px; }
.verdict.lo { background: var(--red-bg); color: var(--red-text); }
.verdict.vuot { background: color-mix(in srgb, var(--up) 18%, transparent); color: var(--up); }
.verdict.trung { background: var(--orange-bg); color: var(--orange-text); }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 0; }
.stats-grid .stat .val { font-size: 18px; }
@media (min-width: 900px) { .stats-grid { grid-template-columns: repeat(4, 1fr); } }

.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 6px 0; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
table.tbl th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; padding: 6px; white-space: nowrap; border-bottom: 1px solid var(--border); }
table.tbl td { padding: 7px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.tbl td.num, table.tbl th.num { text-align: right; }
table.tbl tr.grp td { background: var(--surface-2); font-weight: 650; }
table.tbl tr.small td { opacity: .6; }
table.tbl tr.click { cursor: pointer; }
table.tbl tr.click:active { background: var(--surface-2); }
#bt-equity { height: 220px; margin: 6px 0; }
.hist-item { display: flex; justify-content: space-between; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); cursor: pointer; }
.hist-item:last-child { border-bottom: 0; }
.tag { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 6px; white-space: nowrap; }
.tag.up { background: color-mix(in srgb, var(--up) 18%, transparent); color: var(--up); }
.tag.down { background: color-mix(in srgb, var(--down) 18%, transparent); color: var(--down); }
.tag.flat { background: var(--surface-2); color: var(--muted); }
.news-item { display: block; padding: 9px 0; border-bottom: 1px solid var(--border); color: var(--text); text-decoration: none; font-size: 14.5px; }
.news-item small { color: var(--muted); display: block; margin-top: 2px; }
.sig-item { padding: 10px 0; border-bottom: 1px solid var(--border); }
.sig-item:last-child { border-bottom: 0; }
