:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15181d;
  --muted: #5f6672;
  --border: #dde1e7;
  --accent: #2563eb;
  --up: #15803d;
  --down: #c2410c;
  --neutral: #5f6672;
  --up-bg: #dcfce7;
  --down-bg: #ffedd5;
  --neutral-bg: #eef0f3;
  --line-1: #2563eb;
  --line-2: #d97706;
  --line-3: #9333ea;
  --warn-bg: #fef3c7;
  --warn-text: #78350f;
  --radius: 14px;
  --tabbar-h: 58px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2b3039;
    --accent: #60a5fa;
    --up: #4ade80;
    --down: #fb923c;
    --neutral: #9aa1ad;
    --up-bg: #11301d;
    --down-bg: #3a2210;
    --neutral-bg: #22262e;
    --line-1: #60a5fa;
    --line-2: #fbbf24;
    --line-3: #c084fc;
    --warn-bg: #3a2e0c;
    --warn-text: #fde68a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

a { color: var(--accent); }

.num { font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar .back { margin-left: -8px; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg, .tabbar svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-btn.spinning svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- tab bar ---------- */
.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: 2px;
  color: var(--muted);
  text-decoration: none;
  font-size: 11px;
}
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* ---------- layout ---------- */
main {
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 16px 24px;
}

.banner {
  max-width: 640px;
  margin: 8px auto 0;
  padding: 10px 14px;
  width: calc(100% - 32px);
  border-radius: 12px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
}

.section-title {
  margin: 22px 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card + .card { margin-top: 10px; }

.meta { color: var(--muted); font-size: 13px; }

/* ---------- market summary ---------- */
.summary {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 4px 12px;
}
.summary .label { font-size: 13px; color: var(--muted); }
.summary .big { font-size: 28px; font-weight: 700; line-height: 1.2; }
.summary .changes { text-align: right; font-size: 14px; }

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

/* ---------- stock list ---------- */
.stock-list { list-style: none; margin: 0; padding: 0; }
.stock-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 12px;
  align-items: center;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
}
.stock-list li + li { margin-top: 8px; }
.stock-row:active { background: var(--surface-2); }
.stock-row .name { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stock-row .code { color: var(--muted); font-weight: 400; margin-right: 6px; }
.stock-row .price { text-align: right; font-weight: 600; }
.stock-row .sub { font-size: 13px; color: var(--muted); }
.stock-row .chg { text-align: right; font-size: 13px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge.buy { background: var(--up-bg); color: var(--up); }
.badge.sell { background: var(--down-bg); color: var(--down); }
.badge.hold { background: var(--neutral-bg); color: var(--neutral); }

/* ---------- AI advice ---------- */
details.advice summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}
details.advice summary::-webkit-details-marker { display: none; }
details.advice summary::after { content: "開く"; font-size: 13px; font-weight: 400; color: var(--accent); }
details.advice[open] summary::after { content: "閉じる"; }
.md { font-size: 15px; overflow-wrap: anywhere; }
.md h2 { font-size: 16px; margin: 18px 0 6px; }
.md h3 { font-size: 15px; margin: 14px 0 4px; }
.md p, .md ul { margin: 6px 0; }
.md ul { padding-left: 1.2em; }
.md .table-wrap { overflow-x: auto; margin: 8px 0; }
.md table { border-collapse: collapse; font-size: 13px; white-space: nowrap; }
.md th, .md td { border-bottom: 1px solid var(--border); padding: 4px 8px; text-align: left; }

/* ---------- detail ---------- */
.detail-head > div:first-child { min-width: 0; }
.detail-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.detail-head .big { font-size: 30px; font-weight: 700; line-height: 1.1; }

.chart { margin-top: 12px; }
.chart svg { display: block; width: 100%; height: auto; touch-action: pan-y; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .l-close { stroke: var(--line-1); stroke-width: 2; fill: none; }
.chart .l-sma25 { stroke: var(--line-2); stroke-width: 1.5; fill: none; }
.chart .l-sma75 { stroke: var(--line-3); stroke-width: 1.5; fill: none; }
.chart .l-stop { stroke: var(--down); stroke-width: 1; stroke-dasharray: 4 4; }
.chart .l-take { stroke: var(--up); stroke-width: 1; stroke-dasharray: 4 4; }
.chart .cursor { stroke: var(--muted); stroke-width: 1; }
.chart .dot { fill: var(--line-1); stroke: var(--surface); stroke-width: 2; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 14px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 5px; }
.readout { min-height: 20px; font-size: 13px; color: var(--muted); }

.kv { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; margin: 0; }
.kv dt { font-size: 12px; color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }

ul.plain { margin: 0; padding-left: 1.1em; }
ul.plain li + li { margin-top: 4px; }

/* ---------- news ---------- */
.news-item { display: block; padding: 12px 0; border-bottom: 1px solid var(--border); color: inherit; text-decoration: none; }
.news-item:last-child { border-bottom: 0; }
.news-item .t { font-weight: 600; }

/* ---------- forms ---------- */
label.field { display: block; font-size: 13px; color: var(--muted); }
input[type="password"], input[type="text"], select {
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  font-size: 16px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.btn {
  display: block;
  width: 100%;
  min-height: 48px;
  margin-top: 12px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn:disabled { opacity: 0.5; }

input[type="checkbox"].switch {
  appearance: none;
  -webkit-appearance: none;
  width: 50px;
  height: 30px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  position: relative;
  cursor: pointer;
  flex: none;
}
input[type="checkbox"].switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
input[type="checkbox"].switch:checked { background: var(--accent); border-color: var(--accent); }
input[type="checkbox"].switch:checked::after { transform: translateX(20px); }

.empty { text-align: center; padding: 48px 16px; color: var(--muted); }
.empty .btn { max-width: 280px; margin: 16px auto 0; }

.disclaimer { margin-top: 28px; font-size: 12px; color: var(--muted); text-align: center; }
