:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f8;
  --border: #e3e6ee;
  --text: #1a1d2b;
  --text-muted: #6b7088;
  --primary: #4f46e5;
  --primary-fg: #ffffff;
  --danger: #ef4444;
  --income: #16a34a;
  --expense: #dc2626;
  --shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #273347;
    --border: #334155;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --primary: #818cf8;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
  }
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273347;
  --border: #334155;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --primary: #818cf8;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: none;
  border: none;
  padding: 0;
}

input, select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
}

input[type="color"] {
  padding: 4px;
  height: 40px;
}

input[type="checkbox"] {
  width: auto;
}

input:focus, select:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ロック画面 */
.lock-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(180deg, var(--bg) 0%, var(--surface-2) 100%);
}

.lock-box {
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 24px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
}

.lock-logo {
  font-size: 48px;
  text-align: center;
  margin-bottom: 8px;
}

.lock-box h1 {
  text-align: center;
  margin: 0 0 4px;
  font-size: 24px;
}

.lock-message {
  text-align: center;
  color: var(--text-muted);
  margin: 0 0 20px;
  font-size: 13px;
}

.lock-box form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.lock-help {
  margin-top: 20px;
  font-size: 13px;
  color: var(--text-muted);
}

.lock-help summary {
  cursor: pointer;
}

.lock-help p {
  margin: 12px 0;
}

.error {
  color: var(--danger);
  margin: 0;
  font-size: 13px;
}

/* ボタン */
.btn {
  padding: 11px 16px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 15px;
  text-align: center;
  transition: opacity 0.15s;
  border: 1px solid transparent;
}

.btn.primary {
  background: var(--primary);
  color: var(--primary-fg);
}

.btn.ghost {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text);
}

.btn.danger {
  background: var(--danger);
  color: white;
}

.btn:active { opacity: 0.7; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text);
}

.icon-btn:active { background: var(--surface-2); }

/* アプリ本体 */
.app-screen {
  padding-top: var(--safe-top);
}

.app-bar {
  position: sticky;
  top: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10;
  padding-top: calc(12px + var(--safe-top));
}

.app-bar h1 {
  margin: 0;
  font-size: 18px;
}

.app-bar-actions {
  display: flex;
  gap: 4px;
}

.main {
  flex: 1;
  padding: 16px;
  padding-bottom: calc(80px + var(--safe-bottom));
  overflow-y: auto;
}

.view {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 12px 0 4px;
  padding: 0 4px;
}

.month-selector {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 8px 12px;
  box-shadow: var(--shadow);
}

.month-selector h2 {
  margin: 0;
  font-size: 16px;
}

/* サマリーカード */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.summary-cards .card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 8px;
  text-align: center;
  box-shadow: var(--shadow);
}

.summary-cards .label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.summary-cards .amount {
  display: block;
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.card.income .amount { color: var(--income); }
.card.expense .amount { color: var(--expense); }

/* 口座リスト */
.account-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.account-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
}

.account-item .name {
  display: flex;
  align-items: center;
  gap: 10px;
}

.account-item .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.account-item .balance {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.account-item .balance.negative { color: var(--expense); }

/* 予算リスト */
.budget-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.budget-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: var(--shadow);
}

.budget-item .row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  font-size: 14px;
}

.budget-item .bar {
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}

.budget-item .bar > span {
  display: block;
  height: 100%;
  background: var(--primary);
  transition: width 0.3s;
}

.budget-item.over .bar > span { background: var(--danger); }
.budget-item.over .amount { color: var(--danger); }

/* 取引リスト */
.transaction-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tx-day-group {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.tx-day-header {
  display: flex;
  justify-content: space-between;
  padding: 8px 14px;
  background: var(--surface-2);
  font-size: 12px;
  color: var(--text-muted);
}

.tx-day-header .day-total {
  font-variant-numeric: tabular-nums;
}

.tx-item {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  cursor: pointer;
}

.tx-item:first-child { border-top: none; }
.tx-item:active { background: var(--surface-2); }

.tx-item .cat-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
}

.tx-item .tx-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.tx-item .tx-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tx-item .tx-sub {
  font-size: 11px;
  color: var(--text-muted);
}

.tx-item .tx-amount {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.tx-item .tx-amount.income { color: var(--income); }
.tx-item .tx-amount.expense { color: var(--expense); }
.tx-item .tx-amount.transfer { color: var(--text-muted); }

/* フィルタ */
.filter-bar {
  display: flex;
  gap: 6px;
  align-items: center;
}

.filter-bar > * {
  flex: 1;
  min-width: 0;
}

.filter-bar span {
  flex: 0 0 auto;
  color: var(--text-muted);
}

/* チャート */
.chart-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  min-height: 100px;
}

.chart-box svg {
  width: 100%;
  height: auto;
  display: block;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-top: 8px;
  font-size: 12px;
}

.chart-legend .item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chart-legend .dot {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.chart-empty {
  color: var(--text-muted);
  text-align: center;
  padding: 40px 0;
  font-size: 13px;
}

/* 設定 */
.settings-group {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-group h3 {
  margin: 0 0 4px;
  font-size: 14px;
  color: var(--text-muted);
}

.settings-group .row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.settings-group .row > span {
  flex: 0 0 auto;
  min-width: 80px;
  color: var(--text-muted);
  font-size: 13px;
}

.settings-group .row > input,
.settings-group .row > select {
  flex: 1;
}

.edit-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.edit-list .item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: var(--surface-2);
  border-radius: 8px;
  cursor: pointer;
}

.edit-list .item .dot {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  flex: 0 0 auto;
}

.edit-list .item .label {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.edit-list .item .label .name {
  font-weight: 500;
}

.edit-list .item .label .sub {
  font-size: 11px;
  color: var(--text-muted);
}

.edit-list .item .extra {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.footer-text {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  margin: 8px 0 0;
}

/* タブ */
.tabs {
  display: flex;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
}

.tabs .tab {
  flex: 1;
  padding: 8px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-muted);
  font-weight: 500;
}

.tabs .tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.kind-tabs .tab[data-tx-kind="expense"].active { color: var(--expense); }
.kind-tabs .tab[data-tx-kind="income"].active { color: var(--income); }

/* FAB */
.fab {
  position: fixed;
  right: 16px;
  bottom: calc(72px + var(--safe-bottom));
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  font-size: 28px;
  font-weight: 300;
  box-shadow: 0 4px 16px rgba(79, 70, 229, 0.4);
  z-index: 20;
}

.fab:active { transform: scale(0.95); }

/* 下部ナビ */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding-bottom: var(--safe-bottom);
  z-index: 15;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 0 8px;
  color: var(--text-muted);
  font-size: 11px;
}

.nav-btn span {
  font-size: 20px;
}

.nav-btn.active {
  color: var(--primary);
}

/* モーダル */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.15s;
}

@keyframes fadeIn { from { opacity: 0; } }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } }

.modal-content {
  background: var(--surface);
  width: 100%;
  max-width: 480px;
  max-height: 92dvh;
  border-radius: 16px 16px 0 0;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.2s;
  padding-bottom: var(--safe-bottom);
}

@media (min-width: 640px) {
  .modal { align-items: center; padding: 24px; }
  .modal-content { border-radius: 16px; max-height: 80dvh; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
}

.modal-content form {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}

.modal-content .row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-content .row > span {
  flex: 0 0 80px;
  font-size: 13px;
  color: var(--text-muted);
}

.modal-content .row > input,
.modal-content .row > select {
  flex: 1;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.modal-actions .btn {
  flex: 1;
}

/* トースト */
.toast {
  position: fixed;
  bottom: calc(140px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  color: white;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 200;
  animation: toastIn 0.2s;
  max-width: 90vw;
}

@keyframes toastIn { from { transform: translate(-50%, 10px); opacity: 0; } }

.toast.error { background: var(--danger); }
