/* ===== テーマ（ライト / ダーク）。チャート色も chart.js がここから読む ===== */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e9e8e2;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, 0.1);
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #4a3aa7;
  --s4: #c98500;
  --s5: #d55181;
  --up: #0f8a5f;
  --down: #e34948;
  --up-a: rgba(15, 138, 95, 0.35);
  --down-a: rgba(227, 73, 72, 0.35);
  --up-text: #0a6e4b;
  --down-text: #c0302f;
  --bull: #256abf;
  --bear: #d03b3b;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #b77b00;
  --warning-soft: rgba(250, 178, 25, 0.16);
  --critical: #d03b3b;
  --critical-soft: rgba(208, 59, 59, 0.1);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --header-h: 64px;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #8f8d86;
    --grid: #262624;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --accent-soft: rgba(57, 135, 229, 0.16);
    --s1: #3987e5;
    --s2: #d95926;
    --s3: #9085e9;
    --s4: #c98500;
    --s5: #d55181;
    --up: #199e70;
    --down: #e66767;
    --up-a: rgba(25, 158, 112, 0.4);
    --down-a: rgba(230, 103, 103, 0.4);
    --up-text: #3ccf95;
    --down-text: #ff8f8a;
    --bull: #6da7ec;
    --bear: #e66767;
    --good: #0ca30c;
    --good-text: #3fc43f;
    --warning: #fab219;
    --warning-soft: rgba(250, 178, 25, 0.14);
    --critical: #e25555;
    --critical-soft: rgba(226, 85, 85, 0.14);
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242422;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #8f8d86;
  --grid: #262624;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.16);
  --s1: #3987e5;
  --s2: #d95926;
  --s3: #9085e9;
  --s4: #c98500;
  --s5: #d55181;
  --up: #199e70;
  --down: #e66767;
  --up-a: rgba(25, 158, 112, 0.4);
  --down-a: rgba(230, 103, 103, 0.4);
  --up-text: #3ccf95;
  --down-text: #ff8f8a;
  --bull: #6da7ec;
  --bear: #e66767;
  --good: #0ca30c;
  --good-text: #3fc43f;
  --warning: #fab219;
  --warning-soft: rgba(250, 178, 25, 0.14);
  --critical: #e25555;
  --critical-soft: rgba(226, 85, 85, 0.14);
  --shadow: none;
}

/* ===== ベース ===== */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.55;
  min-height: 100vh;
}
h1, h2, h3, h4 { margin: 0; font-weight: 650; line-height: 1.3; }
h2 { font-size: 15px; }
h3 { font-size: 14px; }
p { margin: 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }
.num, .table td.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.sub { color: var(--ink-2); }
.up { color: var(--up-text); }
.down { color: var(--down-text); }
.flat { color: var(--ink-2); }

.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 100; }
.skip:focus { left: 8px; }

/* ===== ヘッダー ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: var(--header-h);
  padding: 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { width: 32px; height: 32px; flex: none; }
.logo rect { fill: var(--accent); }
.logo path { stroke: #fff; stroke-width: 3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.brand h1 { font-size: 16px; white-space: nowrap; }
.tagline { font-size: 11px; color: var(--muted); white-space: nowrap; }
.tabs { display: flex; gap: 4px; margin-left: auto; background: var(--surface-2); padding: 3px; border-radius: 10px; }
.tab {
  border: 0;
  background: transparent;
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  white-space: nowrap;
}
.tab[aria-current="page"] { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--border); }
.stamp { font-size: 12px; color: var(--ink-2); white-space: nowrap; font-variant-numeric: tabular-nums; }

.banner { margin: 12px 16px 0; padding: 10px 14px; border-radius: 10px; font-size: 13px; }
.banner.warn { background: var(--warning-soft); color: var(--ink); border: 1px solid color-mix(in srgb, var(--warning) 45%, transparent); }
.banner.error { background: var(--critical-soft); color: var(--ink); border: 1px solid color-mix(in srgb, var(--critical) 45%, transparent); }
.banner.error code { font-size: 12px; }

main { padding: 14px 16px 24px; max-width: 1680px; margin: 0 auto; }

/* ===== 共通部品 ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 2px 8px; margin-bottom: 10px; flex-wrap: wrap; }
.card-head .note { font-size: 11px; color: var(--muted); }

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
a.btn { display: inline-block; text-decoration: none; color: var(--ink); }
.btn:hover { border-color: color-mix(in srgb, var(--ink) 25%, transparent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; padding: 4px 6px; border-radius: 6px; color: var(--ink-2); line-height: 1; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

.input, .select, .textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--page);
  font-size: 14px;
}
.textarea { min-height: 88px; resize: vertical; }
label.field { display: grid; gap: 4px; font-size: 12px; color: var(--ink-2); font-weight: 600; }
label.field .req { color: var(--critical); }

.seg { display: inline-flex; background: var(--surface-2); padding: 3px; border-radius: 9px; gap: 2px; flex-wrap: wrap; }
.seg button {
  border: 0;
  background: transparent;
  padding: 5px 12px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
}
.seg button[aria-pressed="true"], .seg button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--border); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border);
  background: transparent;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  color: var(--ink-2);
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 50%, transparent); color: var(--ink); font-weight: 600; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  border: 1px solid transparent;
}
.badge.buy { background: var(--accent-soft); color: var(--ink); border-color: color-mix(in srgb, var(--accent) 50%, transparent); }
.badge.exit { background: var(--warning-soft); color: var(--ink); border-color: color-mix(in srgb, var(--warning) 50%, transparent); }
.badge.bullish { color: var(--up-text); border-color: currentColor; }
.badge.bearish { color: var(--down-text); border-color: currentColor; }
.badge.neutral { color: var(--ink-2); border-color: var(--border); }
.badge.sample { background: var(--warning-soft); color: var(--ink); }

.hint { font-size: 12px; color: var(--muted); }
.empty { padding: 18px 8px; text-align: center; color: var(--muted); font-size: 13px; }

/* ===== ダッシュボードのレイアウト ===== */
.dashboard {
  display: grid;
  gap: 14px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "chart" "side" "trades" "news";
}
.chart-card { grid-area: chart; }
.side { grid-area: side; display: grid; gap: 14px; align-content: start; }
.trades-card { grid-area: trades; }
.news-card { grid-area: news; }

@media (min-width: 720px) and (max-width: 1179px) {
  .side { grid-template-columns: 1fr 1fr; }
}

@media (min-width: 1180px) {
  .dashboard {
    grid-template-columns: 290px minmax(0, 1fr) 340px;
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
      "watch chart side"
      "watch trades side"
      "watch news side";
  }
  .watch {
    grid-area: watch;
    position: sticky;
    top: calc(var(--header-h) + 14px);
    max-height: calc(100vh - var(--header-h) - 28px);
    align-self: start;
  }
  .only-drawer { display: none !important; }
}

/* 1180px 未満では監視リストはドロワー */
@media (max-width: 1179px) {
  .watch {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    width: min(380px, 92vw);
    border-radius: 0 var(--radius) var(--radius) 0;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    padding-top: max(14px, env(safe-area-inset-top));
  }
  .dashboard.drawer-open .watch { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 55; background: rgba(0, 0, 0, 0.45); }
}
@media (prefers-reduced-motion: reduce) { .watch { transition: none; } }

/* ===== 監視リスト ===== */
.watch { display: flex; flex-direction: column; gap: 10px; overflow: hidden; }
.watch-head { display: flex; align-items: center; justify-content: space-between; }
.watch .seg { display: flex; }
.watch .seg button { flex: 1; }
.watch-tools { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); min-height: 26px; }
.watch-list { list-style: none; margin: 0 -8px; padding: 0; overflow-y: auto; flex: 1; min-height: 120px; }
.watch-item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 4px 8px;
  align-items: center;
  padding: 8px;
  border-radius: 10px;
  cursor: pointer;
}
.watch-item:hover { background: var(--surface-2); }
.watch-item[aria-current="true"] { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.watch-item .star { font-size: 16px; color: var(--muted); padding: 2px 4px; }
.watch-item .star[aria-pressed="true"] { color: var(--s4); }
.wi-name { min-width: 0; }
.wi-name b { display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wi-name span { font-size: 11px; color: var(--muted); }
.wi-price { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }
.wi-price small { display: block; font-size: 11px; }
.wi-badges { grid-column: 2 / 4; display: flex; gap: 4px; flex-wrap: wrap; }
.wi-badges:empty { display: none; }
.wi-edit { display: flex; gap: 2px; }

/* ===== チャートカード ===== */
.asset-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.asset-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; flex-wrap: wrap; }
.asset-title h2 { font-size: 18px; }
.asset-sym { font-size: 12px; color: var(--muted); }
.asset-price { margin-left: auto; display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.asset-price .price { font-size: 22px; font-weight: 700; }
.asset-price .chg { font-size: 14px; font-weight: 600; }
.asset-price .asof { font-size: 11px; color: var(--muted); }
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin: 12px 0 8px; }
.toggles { display: flex; flex-wrap: wrap; gap: 6px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px 3px 8px; cursor: pointer; user-select: none; }
.toggle input { accent-color: var(--accent); margin: 0; }
.toggle .sw { width: 10px; height: 3px; border-radius: 2px; display: inline-block; }
.legend { min-height: 58px; font-size: 12px; display: grid; gap: 2px; margin-bottom: 6px; }
.lg-row { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: center; }
.lg-row.sub { color: var(--ink-2); }
.lg-item { display: inline-flex; align-items: center; gap: 4px; font-variant-numeric: tabular-nums; }
.lg-item b { font-weight: 600; }
.lg-k { color: var(--muted); }
.lg-date { font-weight: 700; font-variant-numeric: tabular-nums; }
.lg-chg { font-weight: 600; font-variant-numeric: tabular-nums; }
.sw { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }
.chart-wrap { height: clamp(380px, 62vh, 700px); margin: 0 -4px; }
#chart { width: 100%; height: 100%; }
.sub-tabs { margin-top: 10px; }
@media (max-width: 719px) {
  .chart-wrap { height: clamp(340px, 58vh, 520px); }
  .asset-price { margin-left: 0; width: 100%; justify-content: flex-start; }
  .brand .tagline { display: none; }
}

/* ===== 判定パネル ===== */
.status {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  margin-bottom: 12px;
}
.status .ico { font-size: 18px; line-height: 1.2; }
.status b { display: block; font-size: 14px; }
.status p { font-size: 12px; color: var(--ink-2); }
.status.buy { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.status.exit { background: var(--warning-soft); box-shadow: inset 3px 0 0 var(--warning); }

.checks { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 6px; }
.checks li { display: grid; grid-template-columns: 20px 1fr auto; gap: 6px; align-items: start; font-size: 13px; }
.checks .mark { font-weight: 700; text-align: center; }
.checks .ok .mark { color: var(--good-text); }
.checks .ng .mark { color: var(--muted); }
.checks .val { font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 12px; }
.checks .subs { grid-column: 2 / 4; font-size: 11px; color: var(--muted); }

.zgauge { position: relative; height: 54px; margin: 6px 6px 0; }
.zg-track { position: absolute; left: 0; right: 0; top: 18px; height: 6px; border-radius: 3px; background: var(--grid); }
.zg-zone { position: absolute; top: 18px; height: 6px; border-radius: 3px; }
.zg-tick { position: absolute; top: 12px; width: 1px; height: 18px; background: var(--axis); }
.zg-tick span { position: absolute; top: 20px; transform: translateX(-50%); font-size: 10px; color: var(--muted); white-space: nowrap; }
.zg-legend { margin-top: 2px; font-size: 11px; }
.zg-dot { position: absolute; top: 13px; width: 16px; height: 16px; margin-left: -8px; border-radius: 50%; background: var(--ink); border: 3px solid var(--surface); box-shadow: 0 0 0 1px var(--ink); }
.zg-dot span { position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%); font-size: 11px; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }

.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.stat .k { font-size: 11px; color: var(--muted); }
.stat .v { font-size: 20px; font-weight: 700; }
.stat .v small { font-size: 12px; font-weight: 600; color: var(--ink-2); margin-left: 2px; }
.kinds { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--ink-2); margin-top: 8px; }
.open-trade { margin-top: 8px; font-size: 12px; padding: 8px 10px; border-radius: 8px; border: 1px dashed var(--border); }

.score-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 8px; }
.score-num { font-size: 32px; font-weight: 750; }
.score-items { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.score-items li { display: grid; grid-template-columns: 1fr 64px 40px; gap: 8px; align-items: center; font-size: 12px; }
.score-items .lbl b { display: block; font-size: 12px; }
.score-items .lbl span { color: var(--muted); font-size: 11px; }
.pts-bar { position: relative; height: 8px; background: var(--grid); border-radius: 4px; }
.pts-bar::before { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--axis); }
.pts-bar i { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.pts { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

.ai-result { display: grid; gap: 10px; margin-top: 10px; }
.ai-result h4 { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.ai-result ul { margin: 0; padding-left: 18px; display: grid; gap: 3px; font-size: 13px; }
.ai-result p { font-size: 13px; }
.ai-meta { font-size: 11px; color: var(--muted); }
.ai-error { font-size: 12px; color: var(--ink); background: var(--critical-soft); padding: 8px 10px; border-radius: 8px; margin-top: 8px; }
.disclaimer { font-size: 11px; color: var(--muted); margin-top: 10px; }

/* ===== テーブル ===== */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
.table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.table th, .table td { padding: 7px 6px; border-bottom: 1px solid var(--grid); text-align: left; white-space: nowrap; }
.table th { font-size: 11px; color: var(--muted); font-weight: 600; }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table td.wrap { white-space: normal; min-width: 180px; }
.table tr:last-child td { border-bottom: 0; }

/* ===== ニュース ===== */
.news-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.news-list li a { display: block; padding: 8px; border-radius: 8px; text-decoration: none; color: var(--ink); }
.news-list li a:hover { background: var(--surface-2); }
.news-list .meta { font-size: 11px; color: var(--muted); }
.links { display: flex; flex-wrap: wrap; gap: 8px; }

/* ===== 投資ノート ===== */
.notes-grid { display: grid; gap: 14px; }
@media (min-width: 1040px) {
  .notes-grid { grid-template-columns: 400px minmax(0, 1fr); align-items: start; }
}
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.stats.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 719px) { .stats.four { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.notes-right { display: grid; gap: 14px; }
.row-actions { display: flex; gap: 4px; }

/* ===== 設定 ===== */
.settings-wrap { max-width: 780px; margin: 0 auto; display: grid; gap: 14px; }
.settings-wrap .card p { font-size: 13px; color: var(--ink-2); }
.settings-wrap ol, .settings-wrap ul { font-size: 13px; color: var(--ink-2); padding-left: 20px; margin: 6px 0; }
.key-row { display: flex; gap: 8px; }
.key-row .input { flex: 1; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; font-size: 13px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }

/* ===== フッター・トースト ===== */
.footer { max-width: 1680px; margin: 0 auto; padding: 8px 16px 28px; font-size: 11px; color: var(--muted); display: grid; gap: 4px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  background: var(--ink);
  color: var(--page);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 90;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--critical); color: #fff; }

@media (max-width: 719px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; }
  .tabs { order: 3; width: 100%; margin-left: 0; }
  .tab { flex: 1; padding: 6px 8px; }
  .stamp { margin-left: auto; font-size: 11px; }
  :root { --header-h: 104px; }
  main { padding: 10px 10px 20px; }
  .card { padding: 12px; }
}
