@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=Noto+Sans+JP:wght@400;500;700&display=swap');

:root {
  /* Core Colors - v1.7 Strict */
  --primary-color: #6366f1;
  --primary-dark: #4f46e5;
  --primary-soft: rgba(99, 102, 241, 0.1);
  
  --secondary-color: #ec4899;
  --secondary-soft: rgba(236, 72, 153, 0.1);
  
  --accent-color: #10b981;
  --accent-soft: rgba(16, 185, 129, 0.1);
  
  --danger-color: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.1);
  
  --warning-color: #f59e0b;
  
  /* Neutral Colors */
  --bg-gradient-start: #f8fafc;
  --bg-gradient-end: #e2e8f0;
  
  --text-main: #0f172a; /* Slate 900 */
  --text-sub: #475569;  /* Slate 600 */
  --text-light: #94a3b8; /* Slate 400 */
  
  --bg-soft: #f1f5f9;
  
  /* Glassmorphism & Cards */
  --glass-bg: rgba(255, 255, 255, 0.8);
  --glass-border: rgba(255, 255, 255, 0.5);
  --glass-shadow: 0 10px 30px -5px rgba(0, 0, 0, 0.05);
  
  --card-bg: rgba(255, 255, 255, 0.9);
  --card-shadow: 0 4px 20px -2px rgba(0, 0, 0, 0.05);
  
  /* Strict Geometry (8pt Grid & Silver Ratio) */
  --wrapper-width: 414px;
  --page-padding: 24px;
  --card-padding: 20px;
  --section-gap: 32px;
  --list-gap: 12px;
  
  --radius-card: 28px;
  --radius-control: 16px;
  --radius-icon: 14px;
  
  --font-family: 'Inter', 'Noto Sans JP', sans-serif;
}

/* Base resets & Theme shell (Safari Stability: Fixed Neutral Cap) */
html {
  background-color: #0f172a !important; /* Fixed base for status bar and overscroll */
}

html, body {
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  padding: 0;
}

/* Status Bar Area: Fixed Neutral Dark Cap */
.mobile-wrapper::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: env(safe-area-inset-top);
  background-color: #0f172a;
  z-index: 2000;
}

/* Dark Mode Variables */
.theme-dark {
  /* Neutral Colors (Inverted) */
  --bg-gradient-start: #0f172a; /* Slate 900 */
  --bg-gradient-end: #1e293b;   /* Slate 800 */
  
  --text-main: #f8fafc; /* Slate 50 */
  --text-sub: #cbd5e1;  /* Slate 300 */
  --text-light: #64748b; /* Slate 500 */
  --bg-soft: rgba(255, 255, 255, 0.1);
  
  /* Glassmorphism & Cards (Dark) */
  --glass-bg: rgba(15, 23, 42, 0.85);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-shadow: 0 10px 30px -5px rgba(0, 0, 0, 0.5);
  
  --card-bg: rgba(30, 41, 59, 0.7); /* Slate 800 alpha */
  --card-shadow: 0 4px 20px -2px rgba(0, 0, 0, 0.3);
}

@media (prefers-color-scheme: dark) {
  .theme-auto {
    /* Neutral Colors (Inverted) */
    --bg-gradient-start: #0f172a; /* Slate 900 */
    --bg-gradient-end: #1e293b;   /* Slate 800 */
    
    --text-main: #f8fafc; /* Slate 50 */
    --text-sub: #cbd5e1;  /* Slate 300 */
    --text-light: #64748b; /* Slate 500 */
    --bg-soft: rgba(255, 255, 255, 0.1);
    
    /* Glassmorphism & Cards (Dark) */
    --glass-bg: rgba(15, 23, 42, 0.85);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-shadow: 0 10px 30px -5px rgba(0, 0, 0, 0.5);
    
    --card-bg: rgba(30, 41, 59, 0.7); /* Slate 800 alpha */
    --card-shadow: 0 4px 20px -2px rgba(0, 0, 0, 0.3);
  }
}

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

a {
  text-decoration: none;
  color: inherit;
}

body {
  background: radial-gradient(circle at top right, var(--bg-gradient-start), var(--bg-gradient-end));
  background-attachment: fixed;
  background-color: var(--bg-gradient-start);
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font-family);
  color: var(--text-main);
  line-height: 1.6;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}

/* --- Layout Containers --- */
.mobile-wrapper {
  width: 100%;
  max-width: var(--wrapper-width);
  background: var(--glass-bg);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--glass-border);
  border-radius: 48px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.15);
  min-height: 844px;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  margin: 20px 0;
}

.content-container {
  padding: 0 var(--page-padding); 
  padding-bottom: 120px;
  flex: 1;
  overflow-y: auto;
  scrollbar-width: none;
}
.content-container::-webkit-scrollbar { display: none; }

/* --- Header --- */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 48px var(--page-padding) 16px;
  position: sticky;
  top: 0;
  background: var(--glass-bg);
  backdrop-filter: blur(15px);
  z-index: 50;
}
.page-title { 
  font-size: 20px; 
  font-weight: 800; 
  letter-spacing: -0.02em; 
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
}
.header-icon {
  min-width: 48px; width: auto; height: 48px; display: flex; align-items: center; justify-content: center;
  padding: 0 12px;
  border-radius: var(--radius-icon); 
  background: var(--card-bg); /* Changed from white for dark mode support */
  color: var(--text-main);
  text-decoration: none; transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: var(--card-shadow);
  flex-shrink: 0;
  z-index: 1;
  white-space: nowrap;
}
.header-icon svg { width: 24px; height: 24px; flex-shrink: 0; }
.header-icon:active { transform: scale(0.92); opacity: 0.8; }
.header-placeholder { width: 48px; height: 48px; flex-shrink: 0; }

/* --- Navigation (Bottom) --- */
.bottom-nav {
  position: absolute; bottom: 0; width: 100%; 
  background: var(--glass-bg);
  backdrop-filter: blur(30px) saturate(180%);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  border-top: 1px solid var(--glass-border); 
  display: flex; justify-content: space-around;
  padding: 12px 10px calc(12px + env(safe-area-inset-bottom)); 
  z-index: 100;
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  color: var(--text-light); text-decoration: none; font-size: 10px; font-weight: 700; width: 64px;
  transition: color 0.2s;
}
.nav-item svg { width: 24px; height: 24px; stroke-width: 2; transition: transform 0.2s; }
.nav-item.active { color: var(--primary-color); }

.nav-fab {
  background: var(--primary-gradient);
  width: 56px; height: 56px; border-radius: 18px;
  display: flex; align-items: center; justify-content: center; color: white;
  margin-top: -36px; box-shadow: 0 12px 24px -6px rgba(99, 102, 241, 0.5);
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.nav-fab:active { transform: scale(0.88) rotate(90deg); }

/* --- Cards --- */
.card { 
  background: var(--card-bg); 
  border: 1px solid var(--glass-border); 
  border-radius: var(--radius-card); 
  padding: var(--card-padding); 
  margin-bottom: var(--section-gap); 
  box-shadow: var(--card-shadow);
  transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}
.card.interactive:active { transform: scale(0.98); }

.bg-primary-gradient { 
  background: var(--primary-gradient) !important; 
  color: white !important; 
  border: none !important;
}

/* --- List Items --- */
.list-item, .transaction-item, .account-card, .settings-item {
  display: flex; justify-content: space-between; align-items: center; 
  padding: 16px var(--card-padding); 
  min-height: 72px; 
  text-decoration: none; color: inherit;
  width: 100%;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.list-item:active, .transaction-item:active, .settings-item:active {
  background-color: rgba(0, 0, 0, 0.05);
}

.icon-box {
  width: 48px; height: 48px; border-radius: var(--radius-icon); 
  display: flex; align-items: center; justify-content: center; 
  margin-right: 16px; flex-shrink: 0;
}
.icon-box svg { width: 24px; height: 24px; stroke-width: 2; }
.icon-expense { background-color: var(--secondary-soft); color: var(--secondary-color); }
.icon-income { background-color: var(--accent-soft); color: var(--accent-color); }
.icon-transfer { background-color: var(--primary-soft); color: var(--primary-color); }
.icon-neutral { background-color: var(--bg-soft); color: var(--text-main); }

.item-info { flex: 1; display: flex; flex-direction: column; justify-content: center; min-width: 0; }
.item-title { font-weight: 700; font-size: 16px; color: var(--text-main); margin-bottom: 2px; }
.item-subtext { font-size: 14px; color: var(--text-sub); }
.item-right { text-align: right; display: flex; flex-direction: column; align-items: flex-end; padding-left: 12px; }

/* --- Typography --- */
h1 { font-size: 28px; font-weight: 800; margin-bottom: 8px; letter-spacing: -0.02em; }
h2 { font-size: 20px; font-weight: 700; margin-bottom: 12px; }
.text-main { color: var(--text-main); }
.text-sub { color: var(--text-sub); }
.text-light { color: var(--text-light); }
.font-black { font-weight: 800; }

.amount-wrapper { display: inline-flex; align-items: baseline; font-weight: 800; white-space: nowrap; font-family: var(--font-family); }
.amount-wrapper .currency { font-size: 0.6em; margin-right: 4px; font-weight: 700; }
.amount-wrapper.large { font-size: 36px; }
.amount-wrapper.medium { font-size: 24px; }
.amount-wrapper.small { font-size: 16px; }

/* Amount Input Specific */
.amount-input-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 0;
  width: 100%;
}

.amount-input-wrapper {
  display: flex;
  align-items: baseline;
  justify-content: center;
  font-weight: 800;
  color: var(--text-main);
  width: 100%;
}

.amount-input-currency {
  font-size: 24px;
  margin-right: 8px;
  font-weight: 700;
  color: var(--primary-color);
}

.amount-input {
  border: none;
  background: transparent !important;
  font-size: 36px;
  font-weight: 800;
  width: 100%;
  text-align: center;
  outline: none;
  font-family: var(--font-family);
  padding: 0 8px;
  color: var(--text-main);
  transition: all 0.2s;
}

.amount-input[readonly] {
  color: var(--text-main);
  opacity: 0.6;
  cursor: default;
}

/* --- Form Components --- */
.btn {
  height: 56px; width: 100%; border-radius: var(--radius-control); border: none;
  font-size: 16px; font-weight: 700; cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex; align-items: center; justify-content: center; gap: 8px;
  white-space: nowrap;
}
.btn-primary { 
  background: var(--primary-gradient); 
  color: white; 
  box-shadow: 0 8px 20px -6px rgba(99, 102, 241, 0.4);
}
.btn-primary:active { transform: scale(0.98); }
.btn-secondary { background: var(--primary-soft); color: var(--primary-color); }
.btn-danger-soft { background: var(--danger-soft); color: var(--danger-color); }

.form-input {
  width: 100%; height: 48px; min-height: 48px; padding: 0 var(--card-padding); border-radius: var(--radius-control); 
  border: none;
  background: transparent;
  color: var(--text-main); 
  font-size: 16px; font-weight: 500; outline: none; transition: all 0.2s;
}

/* Fix alignment inside list items */
.item-info .form-input {
  padding-left: 0;
  padding-right: 12px;
}

.form-input::placeholder {
  color: var(--text-light);
  opacity: 0.7;
}

.form-input:focus { 
  background: var(--bg-soft);
}

/* Modern Select Styles */
select.form-input {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2394a3b8' stroke-width='2.5'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--card-padding) center;
  background-size: 18px;
  padding-right: 52px !important;
  cursor: pointer;
}

.item-info select.form-input {
  background-position: right 4px center;
  padding-right: 32px !important;
}

body.theme-dark select.form-input {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%23cbd5e1' stroke-width='2.5'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
}

body.theme-dark .slider {
  background-color: rgba(255, 255, 255, 0.1);
}

/* Animation: Ripple Effect for Checkbox/Switch */
@keyframes ripple {
  0% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.4); }
  100% { box-shadow: 0 0 0 20px rgba(16, 185, 129, 0); }
}
.custom-checkbox:checked { animation: ripple 0.4s ease-out; }

/* Switch Component */
.switch {
  position: relative;
  display: inline-block;
  width: 52px;
  height: 32px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #e2e8f0;
  transition: .3s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 32px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .3s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

input:checked + .slider {
  background-color: var(--accent-color);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

/* --- Utilities --- */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.grid-span-2 {
  grid-column: span 2;
  min-width: 0; /* Critical for preventing overflow */
}

.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.gap-2 { gap: 8px; }
.gap-4 { gap: 16px; }
.text-center { text-align: center; }
.text-xs { font-size: 14px; }
.text-sm { font-size: 14px; }
.text-danger { color: var(--danger-color); }
.text-accent { color: var(--accent-color); }
.p-0 { padding: 0 !important; }
.px-5 { padding-left: var(--card-padding) !important; padding-right: var(--card-padding) !important; }
.mb-2 { margin-bottom: 8px; }
.mt-4 { margin-top: 16px; }
.border-bottom { border-bottom: 1px solid rgba(0,0,0,0.05); }
.overflow-hidden { overflow: hidden; }

/* --- Mobile Optimization (Smartphones) --- */
@media (max-width: 480px) {
  :root {
    --page-padding: 28px; /* Increased to avoid corner clipping */
    --card-padding: 24px;
    --section-gap: 24px;
  }

  body {
    padding: 0;
    background: var(--bg-gradient-start);
    align-items: stretch; /* 中央揃え解除 */
    overflow: hidden; /* Bodyスクロールを防ぎ、内部スクロールのみにする */
  }

  .mobile-wrapper {
    max-width: none;
    width: 100%;
    height: 100vh;
    height: 100dvh; /* iOS Safariのアドレスバー対策 */
    min-height: auto;
    margin: 0;
    border: none;
    border-radius: 0;
    box-shadow: none;
    background: transparent;
  }

  /* ヘッダーの調整 */
  .app-header {
    /* Flexコンテナ内でのSticky配置を確実にする */
    padding-top: max(20px, env(safe-area-inset-top));
    padding-left: 32px;
    padding-right: 32px;
    background: var(--glass-bg); /* 透過度を下げて視認性向上 */
    border-bottom: 1px solid var(--glass-border);
  }

  /* コンテンツエリアの調整 */
  .content-container {
    padding: 20px var(--page-padding) 140px; /* Balanced padding */
  }

  /* ダッシュボードグリッドのスマホ対応 */
  .dashboard-grid {
    /* 強制1列はやめて2列を維持するが、はみ出しを防ぐためにminmax(0, 1fr)を使う */
    grid-template-columns: repeat(2, minmax(0, 1fr)); 
    gap: 8px; /* 隙間を詰めてカードの幅を確保 */
  }
  
  /* ボトムナビの調整 */
  .bottom-nav {
    padding-left: 24px;
    padding-right: 24px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    background: var(--glass-bg);
    border-top: 1px solid var(--glass-border);
  }

  /* リストアイテムのスマホ個別調整 */
  .list-item, .transaction-item {
    padding-left: 24px !important;
    padding-right: 24px !important;
  }
}

/* AI Analysis Screensaver Fix */
#loading-view {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100% !important;
  height: 100% !important;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--glass-bg);
  backdrop-filter: blur(25px) saturate(180%);
  -webkit-backdrop-filter: blur(25px) saturate(180%);
  border-radius: inherit;
  overflow: hidden;
  margin: 0 !important;
}

.ai-scan-line {
  position: absolute;
  left: 0;
  width: 100%;
  height: 150px;
  background: linear-gradient(to bottom, transparent, var(--primary-soft), var(--primary-color), var(--primary-soft), transparent);
  opacity: 0.3;
  animation: scan 4s linear infinite;
  z-index: 1;
}

/* --- Budget Progress Bar --- */
.budget-progress-container {
  width: 100%;
}

.text-xxs {
  font-size: 10px;
}

.progress-bar-bg {
  width: 100%;
  height: 6px;
  background: var(--bg-soft);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.bg-primary { background-color: var(--primary-color); }
.bg-danger { background-color: var(--danger-color); }
