/* my-wealth — mobile first
   ออกแบบให้ใช้บนมือถือเป็นหลัก: การ์ดรายตัวแทนตาราง, ฟอร์มเป็น bottom sheet,
   ปุ่มกดขนาดนิ้วโป้ง (>=44px), เผื่อ safe-area ของ iPhone */

:root {
  --bg: #0b0f16;
  --bg-soft: #0f1520;
  --surface: #151d2c;
  --surface-2: #1b2436;
  --line: #253044;
  --text: #e8edf6;
  --muted: #8b98ae;
  --dim: #63708a;
  --accent: #4f8cff;
  --up: #21d07a;
  --down: #ff5f6d;
  --warn: #ffb347;
  --radius: 16px;
  --radius-sm: 11px;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --sat: env(safe-area-inset-top);
  --sab: env(safe-area-inset-bottom);
  --sal: env(safe-area-inset-left);
  --sar: env(safe-area-inset-right);
}

* { box-sizing: border-box; }

/* กฎ display ของ class ในไฟล์นี้ชนะ user-agent stylesheet
   ถ้าไม่บังคับตรงนี้ องค์ประกอบที่ตั้ง hidden ไว้จะยังโผล่อยู่ */
[hidden] { display: none !important; }

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

body {
  margin: 0;
  background:
    radial-gradient(900px 420px at 10% -10%, #1b2a48 0%, transparent 60%),
    radial-gradient(700px 380px at 95% 2%, #17303a 0%, transparent 56%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  min-height: 100dvh;
  overscroll-behavior-y: none;
  overflow-x: hidden; /* กันหน้าเลื่อนซ้ายขวาบนจอแคบ */
}

body.locked { overflow: hidden; }

h1, h2 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--muted); }
.soft { color: var(--muted); }

/* ── Topbar ─────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(10px + var(--sat)) calc(14px + var(--sar)) 10px calc(14px + var(--sal));
  background: rgba(11, 15, 22, 0.9);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-mark { font-size: 20px; line-height: 1; }
.brand h1 { font-size: 16px; white-space: nowrap; }

#fxChip { cursor: pointer; }

.ccy-toggle {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}
.ccy-toggle button {
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 999px;
  padding: 6px 11px;
  min-height: 30px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.ccy-toggle button.active { background: var(--accent); color: #fff; }

.icon-round {
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  flex: none;
  font: inherit;
  font-size: 17px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 50%;
  cursor: pointer;
}
.icon-round:active { background: var(--surface-2); }
.refresh-icon { display: block; line-height: 1; }
#refreshBtn.loading .refresh-icon { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Layout ─────────────────────────────────────────── */
.wrap {
  padding:
    14px
    calc(14px + var(--sar))
    calc(96px + var(--sab))
    calc(14px + var(--sal));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}

.sec-title { font-size: 14px; color: var(--muted); font-weight: 600; }

.sec-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.sort-wrap select {
  font: inherit;
  font-size: 12.5px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 11px;
  min-height: 34px;
}

/* ── Hero summary ───────────────────────────────────── */
.hero { padding: 16px; }
.hero-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 4px;
}
.hero-label { margin: 0; font-size: 12px; color: var(--muted); }
.hero-value {
  margin: 2px 0 0;
  font-family: var(--mono);
  font-size: clamp(28px, 9vw, 40px);
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1.15;
  word-break: break-all;
}
.hero-alt { margin: 3px 0 0; font-size: 13px; color: var(--dim); font-family: var(--mono); }

/* ใช้ minmax(0, 1fr) ทุกที่แทน 1fr เปล่า ๆ
   เพราะ 1fr มีพื้นขั้นต่ำเป็น min-content ข้อความยาว ๆ จะดันคอลัมน์จนล้นจอมือถือ */
.hero-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.hs { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.hs span, .hs b, .hs i { overflow-wrap: anywhere; }
.hs span { font-size: 11px; color: var(--muted); }
.hs b {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.hs i { font-style: normal; font-size: 11.5px; color: var(--dim); font-family: var(--mono); }

/* ── Allocation ─────────────────────────────────────── */
.allocation { padding: 14px 16px 12px; }
.alloc-bar {
  display: flex;
  height: 10px;
  margin-top: 10px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface-2);
}
.alloc-seg { height: 100%; }

.alloc-legend {
  list-style: none;
  margin: 11px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.alloc-legend li { display: flex; align-items: center; gap: 8px; font-size: 12.5px; }
.dot { width: 9px; height: 9px; border-radius: 3px; flex: none; }
.alloc-legend .pct { margin-left: auto; font-family: var(--mono); color: var(--muted); }

/* ── Holding cards ──────────────────────────────────── */
.holdings { display: flex; flex-direction: column; gap: 10px; }

.holding {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px 14px;
}

.h-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}
.h-id { display: flex; align-items: center; gap: 7px; min-width: 0; overflow: hidden; }
.h-id .sym { overflow: hidden; text-overflow: ellipsis; }
.badge {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.03em;
  border-radius: 5px;
  padding: 3px 6px;
  flex: none;
}
.badge-th { background: rgba(33, 208, 122, 0.14); color: var(--up); }
.badge-global { background: rgba(79, 140, 255, 0.16); color: #7fb0ff; }
.badge-crypto { background: rgba(255, 179, 71, 0.15); color: var(--warn); }

.sym { font-family: var(--mono); font-weight: 600; font-size: 15px; }

.h-right { text-align: right; flex: none; max-width: 60%; }
.h-value { font-family: var(--mono); font-size: 16px; font-weight: 600; letter-spacing: -0.02em; }
.h-pnl { font-family: var(--mono); font-size: 12px; margin-top: 1px; }

.h-name {
  margin: 3px 0 0;
  font-size: 11.5px;
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.h-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px 8px;
  margin-top: 12px;
  padding-top: 11px;
  border-top: 1px solid rgba(37, 48, 68, 0.7);
}
.h-cell { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.h-cell span, .h-cell b { overflow-wrap: anywhere; }
.h-cell span { font-size: 10.5px; color: var(--muted); }
.h-cell b {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
}

@media (min-width: 420px) {
  .h-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

.h-actions { display: flex; gap: 8px; margin-top: 12px; }
.h-actions button {
  flex: 1;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  min-height: 40px;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.h-actions button:active { background: #202b40; }
.h-actions .danger { flex: 0 0 62px; }

.no-cost {
  display: inline-block;
  margin-top: 2px;
  font-size: 11px;
  color: var(--dim);
}
.h-actions .danger:active { color: var(--down); border-color: var(--down); }

.h-err {
  margin-top: 10px;
  font-size: 12px;
  color: var(--warn);
  background: rgba(255, 179, 71, 0.09);
  border-radius: 9px;
  padding: 7px 10px;
}

.src-tag { font-size: 10px; color: var(--dim); }

/* ── Empty / io / foot ──────────────────────────────── */
.empty { text-align: center; padding: 34px 18px; color: var(--muted); }
.empty-icon { font-size: 30px; margin: 0 0 6px; }
.empty-title { margin: 0 0 4px; font-weight: 600; color: var(--text); }
.hint { margin: 0; font-size: 12.5px; color: var(--muted); }

.io-row { display: flex; gap: 8px; margin-top: 14px; }
.io-row .btn { flex: 1; }

.foot { text-align: center; color: var(--dim); font-size: 11.5px; }
.foot p { margin: 3px 0; }

.form-error {
  margin: 0;
  font-size: 13px;
  color: var(--down);
  background: rgba(255, 95, 109, 0.1);
  border: 1px solid rgba(255, 95, 109, 0.3);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

/* ── Buttons ────────────────────────────────────────── */
.btn {
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  min-height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  padding: 10px 16px;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: #3f79e8; }
.btn-ghost { background: var(--surface); border-color: var(--line); color: var(--text); }
.btn-ghost:active { background: var(--surface-2); }
.btn-sm { font-size: 13px; min-height: 40px; padding: 8px 12px; }
.btn-block { width: 100%; }

.fab {
  position: fixed;
  right: calc(16px + var(--sar));
  bottom: calc(18px + var(--sab));
  z-index: 25;
  width: 58px; height: 58px;
  display: grid;
  place-items: center;
  font: inherit;
  font-size: 30px;
  font-weight: 400;
  line-height: 1;
  padding-bottom: 3px;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 50%;
  box-shadow: 0 8px 22px rgba(20, 60, 140, 0.45);
  cursor: pointer;
}
.fab:active { transform: scale(0.94); }

/* ── Bottom sheet ───────────────────────────────────── */
.sheet-root { position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column; justify-content: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(4, 7, 12, 0.66); backdrop-filter: blur(3px); }

.sheet {
  position: relative;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-soft);
  border-top: 1px solid var(--line);
  border-radius: 22px 22px 0 0;
  animation: sheet-up 0.24s cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes sheet-up { from { transform: translateY(100%); } }

.sheet-grab {
  width: 38px; height: 4px;
  margin: 9px auto 0;
  border-radius: 999px;
  background: var(--line);
  flex: none;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  flex: none;
}
.sheet-head h2 { font-size: 16px; }

.sheet-body {
  padding: 0 16px calc(20px + var(--sab));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.row-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field label { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.field-hint { margin: 0; font-size: 11px; color: var(--dim); }

.unit-tag {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 600;
  color: var(--accent);
  background: rgba(79, 140, 255, 0.12);
  border-radius: 4px;
  padding: 1px 5px;
}
.opt { font-size: 10.5px; color: var(--dim); }

.field input {
  font: inherit;
  font-size: 16px; /* กันไม่ให้ iOS ซูมตอนโฟกัส */
  width: 100%;
  min-height: 46px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.field input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 140, 255, 0.18);
}
#fSymbol { font-family: var(--mono); text-transform: uppercase; }

/* สวิตช์เปิด/ปิดการบันทึกต้นทุน */
.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.switch-text { font-size: 13.5px; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.switch-text em { font-style: normal; font-size: 11px; color: var(--dim); line-height: 1.4; }

.switch { position: absolute; opacity: 0; pointer-events: none; }
.switch-track {
  position: relative;
  flex: none;
  width: 46px; height: 28px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  transition: background 0.18s, border-color 0.18s;
}
.switch-track::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.18s, background 0.18s;
}
.switch:checked + .switch-track { background: var(--accent); border-color: var(--accent); }
.switch:checked + .switch-track::after { transform: translateX(18px); background: #fff; }
.switch:focus-visible + .switch-track { box-shadow: 0 0 0 3px rgba(79, 140, 255, 0.3); }

.cost-fields { display: flex; flex-direction: column; gap: 14px; }

.seg {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 4px;
}
.seg-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.seg button {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  min-height: 38px;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.seg button.active { background: var(--accent); color: #fff; }

.sheet-error {
  margin: 0;
  font-size: 13px;
  color: var(--down);
  background: rgba(255, 95, 109, 0.1);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
}

/* ── ปุ่มข่าว + popup ข่าว ───────────────────────────── */
.news-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  font: inherit;
  text-align: left;
  padding: 13px 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
}
.news-btn:active { background: var(--surface-2); }
.news-btn-icon { font-size: 21px; flex: none; line-height: 1; }
.news-btn-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; font-size: 14.5px; font-weight: 600; }
.news-btn-text em { font-style: normal; font-size: 11.5px; font-weight: 400; color: var(--dim); }
.news-btn-arrow { margin-left: auto; font-size: 22px; color: var(--dim); flex: none; }

.news-panel {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-soft);
  padding-top: var(--sat);
}

.news-chips {
  display: flex;
  gap: 7px;
  flex: none;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding: 0 14px 12px;
  border-bottom: 1px solid var(--line);
}
.news-chips::-webkit-scrollbar { display: none; }
.news-chip {
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
  flex: none;
  min-height: 34px;
  padding: 6px 13px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
}
.news-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.news-chip .cnt { opacity: 0.7; font-weight: 400; }

.news-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 14px calc(20px + var(--sab));
}

.news-day {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 12px 0 8px;
  padding: 6px 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: var(--bg-soft);
}

.news-item {
  display: block;
  padding: 11px 0;
  border-bottom: 1px solid rgba(37, 48, 68, 0.6);
  color: inherit;
  text-decoration: none;
}
.news-item:last-child { border-bottom: 0; }
.news-item:active { opacity: 0.6; }
.news-title { font-size: 14px; line-height: 1.45; margin: 0 0 5px; }
.news-meta { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; font-size: 11px; color: var(--dim); }
.news-sym {
  font-family: var(--mono);
  font-weight: 600;
  font-size: 10.5px;
  color: var(--accent);
  background: rgba(79, 140, 255, 0.12);
  border-radius: 4px;
  padding: 2px 6px;
}
.news-status { padding: 30px 6px; text-align: center; color: var(--muted); font-size: 13.5px; }

/* ── Chart ──────────────────────────────────────────── */
.modal-root { justify-content: stretch; }
.chart-panel {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-soft);
  padding-top: var(--sat);
  padding-bottom: var(--sab);
}
.chart-panel .sheet-head h2 { font-family: var(--mono); font-size: 14px; }
.chart-host { flex: 1; min-height: 0; }
.chart-host > div { width: 100%; height: 100%; }

/* ── จอใหญ่ขึ้น: ขยายเป็นหลายคอลัมน์ ────────────────── */
@media (min-width: 700px) {
  body { font-size: 15.5px; }
  .wrap { max-width: 1100px; margin: 0 auto; padding: 22px 24px calc(110px + var(--sab)); gap: 18px; }
  .topbar { padding: 12px 24px; }
  .brand h1 { font-size: 18px; }

  .hero-stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .hero-value { font-size: 40px; }

  .alloc-legend { flex-direction: row; flex-wrap: wrap; gap: 8px 24px; }
  .alloc-legend li { flex: 0 0 auto; }
  .alloc-legend .pct { margin-left: 4px; }

  .holdings { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); }

  .sheet-root { justify-content: center; align-items: center; padding: 24px; }
  .sheet {
    width: min(560px, 100%);
    max-height: 88dvh;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    animation: none;
  }
  .sheet-grab { display: none; }
  .sheet-body { padding-bottom: 20px; }

  .modal-root { padding: 0; }
  .chart-panel { margin: 24px; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
}
