:root {
  --bg: #F5F7FC; --surface: #FFFFFF; --surface2: #ECF0FB; --border: #D8E0F8;
  --accent: #4169E1; --accent2: #E14169; --text: #0E1528; --text2: #5A6FA0;
  --danger: #DC2626; --warn: #F97316; --ok: #16A34A;
  --hitsuyou: #E14169; --shumi: #4169E1;
  --shadow: 0 1px 4px rgba(65,105,225,.08);
  --radius: 12px;
  --font: system-ui, -apple-system, 'Segoe UI', 'Hiragino Sans', 'Yu Gothic UI', sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0D1220; --surface: #161D30; --surface2: #1F2842; --border: #2C3858;
    --accent: #7B9BFF; --accent2: #FF6F93; --text: #E8ECF8; --text2: #93A2C8;
    --danger: #F87171; --warn: #FB923C; --ok: #4ADE80;
    --hitsuyou: #FF6F93; --shumi: #7B9BFF;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body { background: var(--bg); color: var(--text); font-family: var(--font); font-size: 14px; line-height: 1.5; padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
.hidden { display: none !important; }
.accent2 { color: var(--accent2); }

/* header / tabs */
.header { position: sticky; top: 0; z-index: 100; display: flex; align-items: center; gap: 8px; background: var(--surface); border-bottom: 1px solid var(--border); padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top)); }
.header-main { flex: 1; min-width: 0; }
.header h1 { font-size: 16px; font-weight: 700; color: var(--accent); }
.header .sub { font-size: 11px; color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: none; width: 36px; height: 36px; border-radius: 8px; font-size: 18px; cursor: pointer; color: var(--text2); flex: none; }
.icon-btn:hover { background: var(--surface2); }
.menu { position: absolute; right: 12px; top: calc(100% - 4px); background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.15); overflow: hidden; min-width: 240px; }
.menu button, .menu a { display: block; width: 100%; text-align: left; background: none; border: none; padding: 12px 16px; font-size: 14px; color: var(--text); cursor: pointer; }
.menu button:hover, .menu a:hover { background: var(--surface2); }

.tab-bar { position: sticky; top: calc(57px + env(safe-area-inset-top)); z-index: 90; display: flex; background: var(--bg); border-bottom: 1px solid var(--border); }
.tab-btn { flex: 1; background: none; border: none; border-bottom: 3px solid transparent; padding: 10px 2px 8px; font-size: 13px; font-weight: 600; color: var(--text2); cursor: pointer; white-space: nowrap; }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-count { display: inline-block; min-width: 18px; margin-left: 3px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 10px; line-height: 18px; vertical-align: 1px; }
.tab-count:empty { display: none; }

.container { max-width: 820px; margin: 0 auto; padding: 16px; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.error-banner { background: rgba(220,38,38,.1); color: var(--danger); border: 1px solid rgba(220,38,38,.3); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; font-size: 13px; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.card-title { font-size: 13px; font-weight: 700; color: var(--text2); margin-bottom: 12px; }
.hero { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px 16px 18px; margin-bottom: 14px; text-align: center; box-shadow: var(--shadow); }
.hero-label { font-size: 12px; color: var(--text2); }
.hero-amount { font-size: 40px; font-weight: 800; color: var(--accent); letter-spacing: -1px; font-variant-numeric: tabular-nums; }
.hero-amount small { font-size: 16px; font-weight: 600; color: var(--text2); margin-left: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 12px; }
.chip { background: var(--surface2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; font-size: 12px; color: var(--text2); }
.chip b { color: var(--text); font-variant-numeric: tabular-nums; }
.chip .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: 0; }
.chart-wrap { position: relative; height: 220px; }

/* progress items (買い替え) */
.pi { padding: 12px 0; border-bottom: 1px solid var(--border); }
.pi:last-child { border-bottom: none; }
.pi-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.pi-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.pi-pct { font-weight: 700; color: var(--accent2); font-variant-numeric: tabular-nums; }
.pi-meta { font-size: 11px; color: var(--text2); margin-top: 4px; }
.bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 3px; background: linear-gradient(90deg, var(--accent), var(--accent2)); }
.bar-fill.high { background: linear-gradient(90deg, var(--warn), var(--danger)); }
.bar-fill.over { background: var(--danger); }
.badge { display: inline-block; border-radius: 4px; padding: 0 6px; font-size: 10px; font-weight: 700; line-height: 18px; white-space: nowrap; }
.badge-over { background: rgba(220,38,38,.14); color: var(--danger); }
.badge-h { background: rgba(225,65,105,.12); color: var(--hitsuyou); }
.badge-s { background: rgba(65,105,225,.12); color: var(--shumi); }
.badge-w { background: rgba(245,158,11,.15); color: #D97706; }
.badge-off { background: rgba(142,146,164,.15); color: var(--text2); }
.actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.act { background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; font-size: 12px; color: var(--text); cursor: pointer; }
.act:hover { border-color: var(--accent); color: var(--accent); }
.act.warn:hover { border-color: var(--danger); color: var(--danger); }
.empty { color: var(--text2); font-size: 13px; padding: 8px 0; }

/* list */
.search-box { width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px; font-size: 15px; outline: none; margin-bottom: 10px; }
.search-box:focus { border-color: var(--accent); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 8px; }
.filter-bar { display: flex; gap: 6px; flex-wrap: wrap; flex: 1; }
.filter-btn { background: var(--surface2); border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; font-size: 12px; color: var(--text2); cursor: pointer; }
.filter-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.sort-select { background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; font-size: 12px; }
.list-meta { font-size: 12px; color: var(--text2); margin-bottom: 6px; }
.group-head { font-size: 12px; font-weight: 700; color: var(--text2); background: var(--surface2); margin: 10px -16px 0; padding: 5px 16px; display: flex; justify-content: space-between; }
.row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); cursor: pointer; }
.row:last-child { border-bottom: none; }
.row:hover { background: color-mix(in srgb, var(--surface2) 50%, transparent); }
.row.inactive { opacity: .45; }
.row-main { flex: 1; min-width: 0; }
.row-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: 11px; color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-right { text-align: right; flex: none; }
.row-monthly { font-weight: 700; font-variant-numeric: tabular-nums; }
.row-monthly small { font-weight: 400; color: var(--text2); font-size: 11px; }
.row-badges { display: flex; gap: 4px; justify-content: flex-end; margin-top: 2px; }
.wish-price { font-weight: 700; color: var(--accent2); font-variant-numeric: tabular-nums; }
.link { font-size: 12px; margin-left: 4px; }

/* FAB */
.fab { position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom)); width: 58px; height: 58px; border-radius: 50%; border: none; background: var(--accent); color: #fff; font-size: 30px; line-height: 1; box-shadow: 0 6px 18px rgba(65,105,225,.4); cursor: pointer; z-index: 150; }

/* overlays */
.overlay { position: fixed; inset: 0; background: rgba(8,12,24,.55); z-index: 300; display: none; align-items: flex-end; justify-content: center; }
.overlay.open { display: flex; }
.sheet { background: var(--surface); width: 100%; max-width: 560px; max-height: 92vh; border-radius: 16px 16px 0 0; display: flex; flex-direction: column; }
@media (min-width: 640px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 16px; }
}
.sheet-head { display: flex; align-items: center; padding: 14px 16px 6px; }
.sheet-head h3 { flex: 1; font-size: 16px; }
.sheet-body { padding: 4px 16px 12px; overflow-y: auto; }
.sheet-foot { display: flex; gap: 8px; padding: 12px 16px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.spacer { flex: 1; }
.switch-row { display: flex; gap: 8px; margin: 4px 0 8px; }
.switch-item { flex: 1; display: flex; align-items: center; justify-content: space-between; gap: 6px; background: var(--surface2); border-radius: 10px; padding: 8px 10px; font-size: 13px; cursor: pointer; }
.sw { appearance: none; width: 40px; height: 24px; border-radius: 12px; background: var(--border); position: relative; cursor: pointer; flex: none; transition: background .15s; }
.sw::after { content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.sw:checked { background: var(--accent); }
.sw:checked::after { transform: translateX(16px); }
.field { display: block; margin-top: 10px; flex: 1; min-width: 0; }
.field > span { display: block; font-size: 12px; color: var(--text2); margin-bottom: 4px; }
.field input, .field textarea { width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; font-size: 16px; outline: none; }
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field input.error { border-color: var(--danger); }
.field-row { display: flex; gap: 10px; }
.req { color: var(--danger); font-weight: 400; }
.freq-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.freq-chips button { background: var(--surface2); border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; font-size: 12px; color: var(--text2); cursor: pointer; }
.freq-chips button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.preview { margin-top: 14px; background: var(--surface2); border-radius: 10px; padding: 10px 12px; font-size: 13px; color: var(--text2); display: flex; flex-wrap: wrap; gap: 4px 16px; }
.preview b { color: var(--text); font-size: 15px; font-variant-numeric: tabular-nums; }
.btn { background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: 9px 16px; font-size: 14px; cursor: pointer; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); background: none; }
.btn:disabled { opacity: .5; cursor: default; }

.dialog { background: var(--surface); border-radius: 16px; padding: 20px; width: calc(100% - 32px); max-width: 380px; margin-bottom: 30vh; }
.overlay#dialogOverlay { align-items: center; }
.dialog h3 { font-size: 16px; }
.dialog-sub { font-size: 12px; color: var(--text2); margin-bottom: 6px; }
.dialog p { font-size: 13px; color: var(--text2); margin-top: 6px; }
.dialog-foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

.login { position: fixed; inset: 0; z-index: 500; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-box { width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px 22px; text-align: center; box-shadow: var(--shadow); }
.login-icon { font-size: 40px; }
.login-box h2 { font-size: 18px; color: var(--accent); margin-top: 4px; }
.login-box p { font-size: 13px; color: var(--text2); margin: 4px 0 16px; }
.login-input { width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px; font-size: 16px; outline: none; }
.login-input:focus { border-color: var(--accent); }
.login-error { color: var(--danger); font-size: 13px; min-height: 20px; margin: 8px 0 4px; }
.login-btn { width: 100%; padding: 11px; }

.toast { position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 13px; z-index: 400; box-shadow: 0 4px 16px rgba(0,0,0,.2); max-width: calc(100% - 32px); }
.toast.error { background: var(--danger); color: #fff; }
