/* ============================================================
   CSS変数・リセット
   ============================================================ */
:root {
  --color-primary:       #4F46E5;
  --color-primary-dark:  #3730A3;
  --color-primary-light: #EEF2FF;
  --color-secondary:     #10B981;
  --color-warn:          #F59E0B;
  --color-error:         #EF4444;
  --color-ok:            #10B981;

  --color-bg:            #F8FAFC;
  --color-surface:       #FFFFFF;
  --color-border:        #E2E8F0;
  --color-text:          #1E293B;
  --color-text-secondary:#64748B;
  --color-text-muted:    #94A3B8;

  --radius-sm:    6px;
  --radius-md:    12px;
  --radius-lg:    16px;
  --radius-xl:    24px;
  --radius-full:  9999px;

  --shadow-sm:  0 1px 3px rgba(0,0,0,0.08);
  --shadow-md:  0 4px 12px rgba(0,0,0,0.10);
  --shadow-lg:  0 8px 24px rgba(0,0,0,0.12);

  --font-sans: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', sans-serif;

  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  overflow: hidden;
}

/* ============================================================
   アプリ全体レイアウト
   ============================================================ */
#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.page {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* スクロール可能エリア */
.scrollable {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

/* ============================================================
   ローディングオーバーレイ
   ============================================================ */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  backdrop-filter: blur(2px);
}

.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ============================================================
   トースト通知
   ============================================================ */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--color-text);
  color: #fff;
  padding: 10px 20px;
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 500;
  z-index: 8888;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  max-width: 85vw;
  text-align: center;
  white-space: nowrap;
  box-shadow: var(--shadow-md);
  pointer-events: none;
}

.toast-show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast-success { background: var(--color-secondary); }
.toast-error   { background: var(--color-error); }
.toast-info    { background: var(--color-primary); }

/* ============================================================
   認証ページ
   ============================================================ */
#page-auth {
  background: linear-gradient(145deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  justify-content: center;
  align-items: center;
  padding: var(--safe-top) 24px var(--safe-bottom);
}

.auth-container {
  width: 100%;
  max-width: 400px;
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: 32px 24px;
  box-shadow: var(--shadow-lg);
}

.auth-logo {
  text-align: center;
  margin-bottom: 24px;
}

.auth-logo-icon {
  font-size: 48px;
  margin-bottom: 8px;
}

.auth-title {
  font-size: 24px;
  font-weight: 700;
  color: var(--color-text);
}

.auth-subtitle {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-top: 4px;
}

.auth-tabs {
  display: flex;
  background: var(--color-bg);
  border-radius: var(--radius-md);
  padding: 3px;
  margin-bottom: 20px;
}

.auth-tab {
  flex: 1;
  padding: 8px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text-muted);
  cursor: pointer;
  transition: all 0.2s;
}

.auth-tab.active {
  background: var(--color-surface);
  color: var(--color-primary);
  box-shadow: var(--shadow-sm);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ============================================================
   フォーム共通
   ============================================================ */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-secondary);
}

.form-input {
  padding: 12px 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: 16px;
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-surface);
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
}

.form-input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,0.12);
}

.form-error {
  background: #FEF2F2;
  color: var(--color-error);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: 13px;
  border: 1px solid #FECACA;
}

.amount-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.amount-input {
  padding-right: 40px !important;
  text-align: right;
  font-size: 20px !important;
  font-weight: 600;
}

.amount-unit {
  position: absolute;
  right: 14px;
  font-size: 14px;
  color: var(--color-text-secondary);
  pointer-events: none;
}

/* ============================================================
   ボタン共通
   ============================================================ */
.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 20px;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(79,70,229,0.3);
}

.btn-primary:hover   { background: var(--color-primary-dark); }
.btn-primary:active  { transform: scale(0.97); box-shadow: none; }

.btn-primary svg {
  width: 18px;
  height: 18px;
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  background: var(--color-primary-light);
  color: var(--color-primary);
  border: none;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-secondary:hover { background: #DDE6FF; }

.btn-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 16px;
  background: #FEF2F2;
  color: var(--color-error);
  border: 1.5px solid #FECACA;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-danger:hover { background: #FEE2E2; }

.btn-danger svg {
  width: 16px;
  height: 16px;
}

.btn-full  { width: 100%; }
.btn-large { padding: 16px 20px; font-size: 16px; }
.btn-sm    { padding: 8px 14px; font-size: 13px; }

/* アイコンボタン */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}

.icon-btn:hover  { background: var(--color-bg); color: var(--color-text); }
.icon-btn:active { transform: scale(0.92); }

.icon-btn svg {
  width: 22px;
  height: 22px;
}

.icon-btn-sm {
  width: 32px;
  height: 32px;
}

.icon-btn-sm svg {
  width: 18px;
  height: 18px;
}

.text-error { color: var(--color-error) !important; }

/* ============================================================
   ホームページ
   ============================================================ */
#page-home {
  background: var(--color-bg);
}

.home-header {
  display: flex;
  align-items: center;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.home-month {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  color: var(--color-text);
}

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

.home-main {
  flex: 1;
  min-height: 0;
  padding: 20px 16px calc(96px + var(--safe-bottom));
}

/* サマリーカード */
.home-summary {
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  border-radius: var(--radius-xl);
  padding: 24px;
  margin-bottom: 20px;
  color: white;
  box-shadow: var(--shadow-md);
  display: block;
}

.status-badge {
  display: inline-block;
  padding: 4px 14px;
  border-radius: var(--radius-full);
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.status-over    { background: rgba(239,68,68,0.25);  color: #FCA5A5; border: 1px solid rgba(239,68,68,0.4); }
.status-warn    { background: rgba(245,158,11,0.25); color: #FCD34D; border: 1px solid rgba(245,158,11,0.4); }
.status-ok      { background: rgba(255,255,255,0.2); color: #fff;    border: 1px solid rgba(255,255,255,0.3); }
.status-good    { background: rgba(16,185,129,0.25); color: #6EE7B7; border: 1px solid rgba(16,185,129,0.4); }
.status-neutral { background: rgba(255,255,255,0.15);color: rgba(255,255,255,0.8); border: 1px solid rgba(255,255,255,0.2); }

.amount-display {
  text-align: center;
  margin: 8px 0 16px;
}

.amount-label {
  font-size: 13px;
  opacity: 0.8;
  margin-bottom: 4px;
}

.amount-value {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: -1px;
  line-height: 1.1;
}

.budget-info {
  margin-top: 4px;
}

.budget-progress {
  height: 6px;
  background: rgba(255,255,255,0.25);
  border-radius: var(--radius-full);
  overflow: hidden;
  margin-bottom: 8px;
}

.budget-progress-bar {
  height: 100%;
  border-radius: var(--radius-full);
  transition: width 0.5s ease;
}

.budget-labels {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  opacity: 0.85;
}

.budget-hint {
  text-align: center;
  font-size: 13px;
  opacity: 0.75;
  margin-top: 4px;
}

/* 支出一覧 */
.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.section-title {
  flex: 1;
  font-size: 16px;
  font-weight: 700;
  color: var(--color-text);
}

.section-count {
  font-size: 12px;
  color: var(--color-text-muted);
  background: var(--color-bg);
  padding: 3px 10px;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
}

.expense-list {
  display: block;
}

.expense-card {
  display: flex;
  align-items: center;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
  min-height: 72px;
  flex-shrink: 0;
  border: 1px solid var(--color-border);
}

.expense-card-left {
  flex: 1;
  min-width: 0;
}

.expense-card-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  flex-shrink: 0;
  margin-left: 12px;
}

.expense-date {
  font-size: 11px;
  color: var(--color-text-muted);
  margin-bottom: 2px;
}

.expense-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.expense-category {
  display: inline-block;
  font-size: 11px;
  color: var(--color-primary);
  background: var(--color-primary-light);
  padding: 2px 8px;
  border-radius: var(--radius-full);
  margin-top: 3px;
}

.expense-user {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 3px;
}

.expense-amount {
  font-size: 17px;
  font-weight: 700;
  color: var(--color-text);
}

.expense-delete-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.expense-delete-btn:hover {
  background: #FEF2F2;
  color: var(--color-error);
}

.expense-delete-btn svg {
  width: 15px;
  height: 15px;
}

/* 空状態 */
.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--color-text-muted);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 15px;
  margin-bottom: 4px;
}

/* 追加ボタン */
.add-btn-container {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  flex-shrink: 0;
}

.add-btn {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: var(--color-primary);
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(79,70,229,0.5);
  transition: transform 0.15s, box-shadow 0.15s;
}

.add-btn:hover  { transform: scale(1.06); box-shadow: 0 6px 24px rgba(79,70,229,0.55); }
.add-btn:active { transform: scale(0.94); box-shadow: 0 2px 10px rgba(79,70,229,0.4); }

.add-btn svg {
  width: 30px;
  height: 30px;
}

/* ============================================================
   サブページ共通（追加・履歴・設定）
   ============================================================ */
#page-add,
#page-history,
#page-settings {
  background: var(--color-bg);
}

.sub-header {
  display: flex;
  align-items: center;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.sub-title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 700;
  color: var(--color-text);
}

.sub-main {
  flex: 1;
  min-height: 0;
  padding: 20px 16px calc(32px + var(--safe-bottom));
}

/* ============================================================
   追加ページ
   ============================================================ */
.add-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
  max-width: 500px;
  margin: 0 auto;
}

/* カテゴリチップ */
.category-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.category-chip {
  padding: 7px 16px;
  background: var(--color-surface);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  flex-shrink: 0;
}

.category-chip:hover    { border-color: var(--color-primary); color: var(--color-primary); }
.category-chip.selected { background: var(--color-primary); border-color: var(--color-primary); color: white; }

/* ============================================================
   履歴ページ
   ============================================================ */
.history-select-wrap {
  margin-bottom: 16px;
}

.history-select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: 16px;
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-surface);
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748B' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 18px;
  padding-right: 40px;
}

.history-summary-card {
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
  border-radius: var(--radius-xl);
  padding: 20px 24px;
  margin-bottom: 16px;
  color: white;
  text-align: center;
  box-shadow: var(--shadow-md);
}

.history-month-title {
  font-size: 14px;
  opacity: 0.85;
  margin-bottom: 8px;
}

.history-total {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: -1px;
  margin-bottom: 8px;
}

.history-budget {
  font-size: 13px;
  opacity: 0.8;
}

.history-balance {
  font-size: 14px;
  font-weight: 600;
  margin-top: 4px;
}

.text-ok { color: #6EE7B7; }

.history-expense-list {
  display: block;
}

.history-expense-card {
  display: flex;
  align-items: flex-start;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
  min-height: 72px;
  flex-shrink: 0;
  border: 1px solid var(--color-border);
}

.history-exp-left {
  flex: 1;
  min-width: 0;
}

.history-exp-right {
  flex-shrink: 0;
  margin-left: 12px;
}

.history-exp-datetime {
  font-size: 11px;
  color: var(--color-text-muted);
  margin-bottom: 2px;
}

.history-exp-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-exp-user {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 4px;
}

.history-exp-amount {
  font-size: 17px;
  font-weight: 700;
  color: var(--color-text);
}

/* ============================================================
   設定ページ
   ============================================================ */
.settings-section {
  margin-bottom: 20px;
}

.settings-section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 10px;
  padding-left: 4px;
}

.settings-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.settings-label {
  font-size: 14px;
  color: var(--color-text-secondary);
  flex-shrink: 0;
  min-width: 80px;
}

.settings-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text);
}

.settings-edit-row {
  display: flex;
  gap: 8px;
  flex: 1;
  align-items: center;
}

.settings-input {
  flex: 1;
  padding: 8px 12px !important;
  font-size: 14px !important;
}

.settings-desc {
  font-size: 12px;
  color: var(--color-text-muted);
}

/* カテゴリ設定 */
.categories-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.category-item {
  display: flex;
  align-items: center;
  background: var(--color-bg);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  border: 1px solid var(--color-border);
}

.category-item-name {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text);
}

.add-category-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.add-category-row .form-input {
  flex: 1;
}

/* ============================================================
   ユーティリティ
   ============================================================ */
.text-muted   { color: var(--color-text-muted) !important; }
.text-center  { text-align: center; }
.loading-text { text-align: center; color: var(--color-text-muted); padding: 48px 0; }
.error-state  { text-align: center; color: var(--color-error); padding: 24px; font-size: 14px; }

/* ============================================================
   レスポンシブ
   ============================================================ */
@media (min-width: 600px) {
  .home-main,
  .sub-main {
    padding-left: calc(max(16px, 50% - 280px));
    padding-right: calc(max(16px, 50% - 280px));
  }

  .add-btn {
    width: 72px;
    height: 72px;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg:            #0F172A;
    --color-surface:       #1E293B;
    --color-border:        #334155;
    --color-text:          #F1F5F9;
    --color-text-secondary:#94A3B8;
    --color-text-muted:    #64748B;
    --color-primary-light: #1E1B4B;
  }

  .home-summary {
    background: linear-gradient(135deg, #4338CA 0%, #312E81 100%);
  }

  .history-summary-card {
    background: linear-gradient(135deg, #4338CA 0%, #312E81 100%);
  }

  #page-auth {
    background: linear-gradient(145deg, #312E81 0%, #1E1B4B 100%);
  }

  .category-chip {
    background: var(--color-surface);
  }

  .history-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2394A3B8' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  }
}
