:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1c1f24;
  --muted: #6a7280;
  --accent: #2f6fdb;
  --accent-text: #ffffff;
  --pos: #12804a;
  --neg: #c62f2f;
  --warn: #b26b00;
  --pos-bg: #e6f4ec;
  --neg-bg: #fbeaea;
  --warn-bg: #fdf1dc;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 12px;
  --gutter: 16px;
  --tabbar-h: 60px;
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a;
    --surface: #1d2025;
    --surface-2: #262a31;
    --border: #323741;
    --text: #e8eaee;
    --muted: #9aa3b1;
    --accent: #6b9cf0;
    --accent-text: #0e1420;
    --pos: #4cc285;
    --neg: #f07171;
    --warn: #e7a740;
    --pos-bg: #173323;
    --neg-bg: #3a1d1d;
    --warn-bg: #3a2c14;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #14161a; --surface: #1d2025; --surface-2: #262a31; --border: #323741; --text: #e8eaee; --muted: #9aa3b1;
  --accent: #6b9cf0; --accent-text: #0e1420; --pos: #4cc285; --neg: #f07171; --warn: #e7a740;
  --pos-bg: #173323; --neg-bg: #3a1d1d; --warn-bg: #3a2c14; --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  font-variant-numeric: tabular-nums;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.3; margin: 0; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }

/* ---------- 上部バーとタブ ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) var(--gutter) 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 17px; font-weight: 700; }
.lock-btn { margin-left: auto; }
.tabs { display: flex; gap: 4px; }
.tabs a {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 999px;
  color: var(--muted); text-decoration: none; font-weight: 600; font-size: 14px;
}
.tabs a.active { background: var(--surface-2); color: var(--text); }
.tabs .ic { font-size: 15px; }

@media (max-width: 720px) {
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    justify-content: space-around; gap: 0;
    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);
  }
  .tabs a { flex-direction: column; gap: 0; font-size: 11px; padding: 6px 0; flex: 1; border-radius: 0; justify-content: center; }
  .tabs a.active { background: none; color: var(--accent); }
  .tabs .ic { font-size: 20px; line-height: 1.2; }
  .app { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px) !important; }
}

/* ---------- レイアウト ---------- */
.app { max-width: 1100px; margin: 0 auto; padding: 16px var(--gutter) 48px; outline: none; }
.stack { display: grid; gap: 16px; }
.grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card > h2 { margin-bottom: 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.xs { font-size: 12px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

/* ---------- 総資産 ---------- */
.hero .label { color: var(--muted); font-size: 13px; font-weight: 600; }
.hero .total { font-size: clamp(30px, 8vw, 40px); font-weight: 750; letter-spacing: -0.02em; margin: 2px 0 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px;
  font-size: 13px; font-weight: 600; background: var(--surface-2);
}
.chip.pos { background: var(--pos-bg); }
.chip.neg { background: var(--neg-bg); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }

/* ---------- 一覧・表 ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.list li:first-child { border-top: none; }
.list .grow { flex: 1; min-width: 0; }
.list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.bar { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; white-space: normal; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
tfoot td { font-weight: 700; border-bottom: none; }

.level { display: inline-block; min-width: 56px; padding: 1px 8px; border-radius: 6px; font-weight: 700; text-align: center; }
.level.ok { background: var(--pos-bg); color: var(--pos); }
.level.warn { background: var(--warn-bg); color: var(--warn); }
.level.bad { background: var(--neg-bg); color: var(--neg); }

/* ---------- 更新ボタン ---------- */
.refresh-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.refresh-item {
  display: grid; gap: 2px; text-align: left; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
}
.refresh-item:hover { border-color: var(--accent); }
.refresh-item .t { font-weight: 700; }
.refresh-item .s { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.refresh-item.ok .s::before { content: "✓ "; color: var(--pos); }
.refresh-item.err .s { color: var(--neg); }
.refresh-item.err .s::before { content: "✕ "; }
.refresh-item.busy { opacity: 0.6; pointer-events: none; }

/* ---------- ボタン・フォーム ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 16px; border-radius: 10px; border: 1px solid transparent;
  background: var(--accent); color: var(--accent-text); font: inherit; font-weight: 700; cursor: pointer;
  text-decoration: none;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: var(--neg); color: #fff; }
.btn.small { min-height: 32px; padding: 4px 12px; font-size: 13px; }
.btn.block { width: 100%; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600; font-size: 13px;
  padding: 6px 12px; border-radius: 8px; cursor: pointer;
}
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

label.field { display: grid; gap: 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
input, select, textarea {
  width: 100%; min-height: 42px; padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 16px; /* iPhone で拡大されないよう 16px */
}
textarea { min-height: 72px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin: 0; }
legend { font-size: 13px; font-weight: 700; padding: 0 6px; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; color: var(--text); }
.check input { width: 20px; min-height: 20px; }

/* ---------- ロック画面 ---------- */
.lock { max-width: 420px; margin: 8vh auto 0; }
.lock .logo { font-size: 40px; text-align: center; }
.lock h1 { text-align: center; font-size: 22px; margin: 8px 0 20px; }

/* ---------- ダイアログ・通知 ---------- */
.dialog {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  border: 1px solid var(--border); border-radius: 16px; padding: 0;
  background: var(--surface); color: var(--text);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog form { padding: 16px; display: grid; gap: 14px; }
.dialog .actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; position: sticky; bottom: 0; background: var(--surface); padding-top: 8px; }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 10px;
  background: var(--text); color: var(--bg); font-weight: 600; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s; z-index: 100;
}
.toast.show { opacity: 1; }
.notice { padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--text); font-size: 14px; }
.notice.bad { background: var(--neg-bg); }
.notice.ok { background: var(--pos-bg); }

.chart-box { position: relative; height: 240px; }
.chart-box.tall { height: 280px; }
.empty { padding: 24px 0; text-align: center; color: var(--muted); }
details summary { cursor: pointer; font-weight: 600; }
