*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --income:      #6FCF97;
  --income-bg:   #F3FAF6;
  --expense:     #FF8A80;
  --expense-bg:  #FFF5F5;
  --balance:     #64B5F6;
  --balance-bg:  #F4F8FF;
  --primary:     #6FCF97;
  --primary-dark:#4CAF7A;
  --bg:          #FAFAFA;
  --card:        #FFFFFF;
  --border:      #EEEEEE;
  --text:        #333333;
  --text-sub:    #777777;
  --header-h:    100px;
  --bottom-h:    64px;
}

html { font-size: 15px; }
body {
  font-family: 'Noto Sans JP', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  padding-bottom: var(--bottom-h);
}

/* ===== HEADER ===== */
.header {
  position: sticky; top: 0; z-index: 50;
  background: #fff;
  border-bottom: 1px solid var(--border);
}
.header-inner {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px 6px;
}
.header-title { font-size: 18px; font-weight: 700; }
.header-year  { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; }
.year-btn { background: none; border: 1px solid var(--border); border-radius: 6px; width: 28px; height: 28px; cursor: pointer; font-size: 16px; display: flex; align-items: center; justify-content: center; }
.year-btn:hover { background: var(--bg); }

.tab-nav {
  display: flex; overflow-x: auto; -webkit-overflow-scrolling: touch;
  padding: 0 12px 0;
  scrollbar-width: none;
}
.tab-nav::-webkit-scrollbar { display: none; }
.tab-btn {
  flex-shrink: 0; background: none; border: none; cursor: pointer;
  padding: 8px 14px; font-size: 13px; color: var(--text-sub);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  transition: color .2s, border-color .2s;
}
.tab-btn.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }

/* ===== MAIN ===== */
.main { max-width: 1400px; margin: 0 auto; padding: 16px; }

.view { display: none; }
.view.active { display: block; }

/* ===== CARD ===== */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 6px rgba(0,0,0,.04);
}

/* ===== MONTH NAV ===== */
.month-nav {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
}
.month-label { font-size: 17px; font-weight: 700; }

/* ===== NET BALANCE CARD ===== */
.net-card {
  text-align: center;
  padding: 28px 20px 32px;
  margin-bottom: 12px;
}
.net-card-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-sub);
  letter-spacing: 0.06em;
  margin-bottom: 14px;
}
.net-card-amount {
  font-size: 40px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.01em;
  color: var(--text);
  transition: color .3s;
}

/* ===== SUMMARY ROW ===== */
.summary-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 12px;
}
.summary-item {
  border-radius: 14px;
  padding: 16px 10px;
  text-align: center;
  border: 1px solid var(--border);
}
.summary-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-sub);
  margin-bottom: 8px;
  letter-spacing: 0.04em;
}
.summary-value {
  font-size: 15px;
  font-weight: 700;
}
.summary-income  { background: var(--income-bg); }
.summary-income  .summary-value { color: var(--income); }
.summary-expense { background: var(--expense-bg); }
.summary-expense .summary-value { color: var(--expense); }
.summary-invest  { background: var(--balance-bg); }
.summary-invest  .summary-value { color: var(--balance); }

/* ===== SECTION TITLE ===== */
.section-title { font-size: 15px; font-weight: 700; margin-bottom: 14px; color: var(--text); letter-spacing: .04em; }
.section-sub   { font-size: 13px; color: var(--text-sub); margin-bottom: 14px; }

/* ===== BREAKDOWN ===== */
.breakdown-chart-wrap {
  display: grid; grid-template-columns: 38% 62%; gap: 20px; align-items: center;
}
.donut-wrap {
  width: 100%; max-width: 360px; aspect-ratio: 1 / 1; margin: 0 auto;
  padding-right: 8px; position: relative; box-sizing: border-box;
}
.donut-wrap canvas { width: 100% !important; height: 100% !important; }
#expense-breakdown { min-width: 0; width: 100%; max-width: 100%; padding-left: 0; padding-right: 0; margin-right: 0; }

.breakdown-row {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px 10px 0; min-height: 52px;
  border-bottom: 1px solid var(--border);
}
.breakdown-row:last-child { border-bottom: none; }
.breakdown-dot   { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; flex-shrink: 0; }
.cat-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 48px; height: 48px;
  background: none; box-shadow: none; padding: 0; border-radius: 0;
  margin-right: 10px; flex-shrink: 0;
}
.cat-icon svg { width: 48px; height: 48px; display: block; }
.cat-icon img { width: 48px; height: 48px; display: block; object-fit: contain; }
.breakdown-name  { min-width: 120px; flex-shrink: 0; font-size: 13px; font-weight: 500; display: flex; align-items: center; }
.breakdown-bar-wrap { flex: 1; min-width: 0; background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.breakdown-bar   { height: 8px; border-radius: 4px; transition: width .4s; }
.breakdown-pct   { font-size: 11px; color: var(--text-sub); width: 36px; flex-shrink: 0; text-align: right; }
.breakdown-amount{ font-size: 13px; font-weight: 600; width: 72px; flex-shrink: 0; text-align: right; padding-right: 0; white-space: nowrap; }

/* ===== BUDGET PROGRESS ===== */
.budget-row { padding: 8px 0; border-bottom: 1px solid var(--border); }
.budget-row:last-child { border-bottom: none; }
.budget-row-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 5px; }
.budget-name     { font-size: 13px; font-weight: 600; }
.budget-amount   { font-size: 12px; color: var(--text-sub); }
.budget-bar-wrap { background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.budget-bar      { height: 8px; border-radius: 4px; background: var(--income); transition: width .4s; }
.budget-bar.over { background: var(--expense); }
.budget-pct      { font-size: 11px; color: var(--text-sub); text-align: right; margin-top: 3px; }
.no-budget-msg   { font-size: 13px; color: var(--text-sub); }
.badge-annual {
  display: inline-block; font-size: 10px; font-weight: 700;
  background: var(--balance-bg); color: var(--balance);
  border-radius: 4px; padding: 1px 5px; margin-left: 4px; vertical-align: middle;
}

/* ===== MONTHLY TABLE ===== */
.table-controls {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px;
}
.table-controls label { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.table-controls select { border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; font-size: 13px; }
.toggle-label { cursor: pointer; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.monthly-table {
  border-collapse: collapse; min-width: 860px; font-size: 12.5px; width: 100%;
}
.monthly-table th, .monthly-table td {
  border: 1px solid var(--border);
  padding: 5px 8px; white-space: nowrap;
}
.monthly-table th {
  background: #f9fafb; font-weight: 600; text-align: center;
  position: sticky; top: 0; z-index: 2;
}
.monthly-table td { text-align: right; }
.monthly-table td.cat-cell { text-align: left; font-weight: 600; background: #f9fafb; }
.monthly-table td.sub-cell { text-align: left; color: var(--text-sub); padding-left: 20px; }
.monthly-table td.zero     { color: #d1d5db; }
.monthly-table tr.cat-row  { background: #f0f9ff; }
.monthly-table tr.total-row{ background: #fef3c7; font-weight: 700; }
.monthly-table .income-val { color: var(--income); }
.monthly-table .expense-val{ color: var(--expense); }

/* ===== ANNUAL ===== */

/* --- ヒーローカード（年間収支） --- */
.annual-hero {
  text-align: center;
  padding: 24px 16px 20px;
}
.annual-hero-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-sub);
  letter-spacing: 0.06em;
  margin-bottom: 10px;
}
.annual-hero-amount {
  font-size: 34px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.01em;
  margin-bottom: 18px;
}
.annual-hero-amount.is-positive { color: var(--income); }
.annual-hero-amount.is-negative { color: var(--expense); }
.annual-hero-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.annual-hero-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  font-size: 11px; color: var(--text-sub);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 6px;
}
.annual-hero-dot { width: 8px; height: 8px; border-radius: 50%; }
.annual-hero-item-label { letter-spacing: 0.04em; }
.annual-hero-item-val { font-size: 14px; font-weight: 700; color: var(--text); }
.income-text  { color: var(--income); }
.expense-text { color: var(--expense); }
.balance-text { color: var(--balance); }

/* --- 月別カード --- */
.annual-month-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.annual-month-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 8px;
  text-align: center;
}
.annual-month-card.is-empty { opacity: 0.45; }
.annual-month-name { font-size: 12px; color: var(--text-sub); margin-bottom: 6px; font-weight: 500; }
.annual-month-net  { font-size: 15px; font-weight: 700; margin-bottom: 6px; }
.annual-month-net.is-positive { color: var(--income); }
.annual-month-net.is-negative { color: var(--expense); }
.annual-month-empty { font-size: 12px; color: var(--text-sub); padding: 8px 0; }
.annual-month-detail {
  display: flex; flex-direction: column; gap: 2px;
  font-size: 10px;
}

/* --- カテゴリ TOP5 --- */
.annual-bar-row {
  display: flex; align-items: center; gap: 8px; padding: 7px 0;
  border-bottom: 1px solid var(--border);
}
.annual-bar-row:last-child { border-bottom: none; }
.annual-bar-rank {
  width: 18px; height: 18px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%; background: var(--bg);
  font-size: 11px; font-weight: 700; color: var(--text-sub);
}
.annual-bar-name   { width: 90px; font-size: 13px; flex-shrink: 0; display: flex; align-items: center; }
.annual-bar-wrap   { flex: 1; background: var(--bg); border-radius: 4px; height: 12px; overflow: hidden; }
.annual-bar        { height: 12px; border-radius: 4px; transition: width .4s; }
.annual-bar-pct    { font-size: 11px; color: var(--text-sub); min-width: 36px; text-align: right; }
.annual-bar-amount { width: 90px; text-align: right; font-size: 13px; font-weight: 600; }

/* --- 年間ハイライト --- */
.annual-highlight-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.annual-highlight-row:last-child { border-bottom: none; }
.annual-highlight-icon { font-size: 18px; flex-shrink: 0; }
.annual-highlight-label { flex: 1; font-size: 13px; color: var(--text-sub); }
.annual-highlight-value { font-size: 13px; font-weight: 700; text-align: right; }

.chart-wrap { height: 300px; position: relative; }
canvas { width: 100% !important; }

/* ===== IMPORT ===== */
.import-steps {
  background: var(--bg); border-radius: 8px; padding: 14px 14px 14px 28px;
  margin-bottom: 16px; font-size: 13px; line-height: 2;
}
.drop-zone {
  border: 2px dashed var(--border); border-radius: 10px;
  padding: 40px 20px; text-align: center; cursor: pointer;
  transition: border-color .2s, background .2s;
}
.drop-zone:hover, .drop-zone.drag-over {
  border-color: var(--primary); background: var(--balance-bg);
}
.drop-icon { font-size: 36px; margin-bottom: 8px; }
.drop-sub  { font-size: 12px; color: var(--text-sub); margin-top: 4px; }

.import-summary {
  background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 8px;
  padding: 12px 16px; margin-bottom: 12px; font-size: 13px;
}
.import-summary strong { color: var(--income); }

.preview-table { border-collapse: collapse; font-size: 12px; width: 100%; min-width: 600px; }
.preview-table th, .preview-table td {
  border: 1px solid var(--border); padding: 5px 8px; white-space: nowrap;
}
.preview-table th { background: #f9fafb; font-weight: 600; }
.preview-table td.income-row  { color: var(--income); }
.preview-table td.expense-row { color: var(--expense); }

.import-actions {
  display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px;
}
.import-result {
  text-align: center; padding: 24px; font-size: 15px; font-weight: 600;
  background: var(--income-bg); border-radius: 10px; color: var(--income);
}

/* ===== BUDGET FORM ===== */
#budget-form { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.budget-input-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.budget-input-row:last-child { border-bottom: none; }
.budget-input-label { width: 80px; font-size: 13px; font-weight: 600; flex-shrink: 0; }
.budget-dual-wrap { display: flex; align-items: center; gap: 8px; flex: 1; flex-wrap: wrap; }
.budget-input-sep { font-size: 13px; color: var(--text-sub); flex-shrink: 0; }
.budget-input-wrap { display: flex; align-items: center; gap: 4px; }
.budget-input-wrap input {
  width: 100px; border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 8px; font-size: 13px; text-align: right;
}
.budget-input-wrap input:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.12);
}
.budget-input-wrap span { font-size: 12px; color: var(--text-sub); white-space: nowrap; }

.budget-total-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 0; margin-top: 4px;
  border-top: 2px solid var(--text); flex-wrap: wrap;
}
.budget-total-label { width: 80px; font-size: 14px; font-weight: 700; flex-shrink: 0; }
.budget-total-val {
  font-size: 14px; font-weight: 700; color: var(--primary);
  min-width: 100px; text-align: right; display: inline-block;
}
.budget-actions { text-align: right; margin-top: 16px; }

/* ===== IMPORT HISTORY ===== */
.history-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.history-item:last-child { border-bottom: none; }
.history-date   { color: var(--text-sub); width: 80px; flex-shrink: 0; }
.history-type   { flex-shrink: 0; }
.history-cat    { flex: 1; }
.history-amount { font-weight: 600; min-width: 70px; text-align: right; }
.history-income { color: var(--income); }
.history-expense{ color: var(--expense); }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex; align-items: center; gap: 4px;
  border: none; border-radius: 8px; padding: 8px 16px;
  font-size: 13px; font-weight: 600; cursor: pointer; transition: opacity .2s;
}
.btn:hover { opacity: .85; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-ghost   { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-sm      { padding: 5px 12px; font-size: 12px; }

/* ===== BOTTOM NAV ===== */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--bottom-h);
  background: #fff; border-top: 1px solid var(--border);
  display: flex; z-index: 50;
}
.bottom-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px;
  background: none; border: none; cursor: pointer;
  color: var(--text-sub); font-size: 10px; font-weight: 500;
  transition: color .2s;
}
.bottom-btn svg { width: 22px; height: 22px; }
.bottom-btn.active { color: var(--primary); }

/* ===== LOADING / TOAST ===== */
#loading-overlay {
  position: fixed; inset: 0; background: rgba(255,255,255,.7);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.spinner {
  width: 36px; height: 36px; border: 3px solid var(--border);
  border-top-color: var(--primary); border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 10px 20px;
  border-radius: 20px; font-size: 13px; z-index: 300;
  white-space: nowrap; pointer-events: none;
}

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 400;
}
.modal-sheet {
  background: var(--card); border-radius: 16px 16px 0 0;
  width: 100%; max-width: 600px; max-height: 85vh;
  display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 16px 12px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h3 { font-size: 15px; font-weight: 700; }
.modal-close {
  background: none; border: none; cursor: pointer; font-size: 18px;
  color: var(--text-sub); padding: 4px 8px; border-radius: 6px;
}
.modal-close:hover { background: var(--bg); }

#records-modal-body { overflow-y: auto; padding: 12px 16px; flex: 1; }

/* Record items */
.record-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.record-item:last-child { border-bottom: none; }
.record-item-info { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.record-date { font-size: 11px; color: var(--text-sub); }
.record-cat  { font-size: 11px; color: var(--text-sub); }
.record-memo {
  font-size: 13px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.record-item-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.record-amount { font-size: 14px; font-weight: 700; min-width: 72px; text-align: right; }
.record-item-actions { display: flex; gap: 2px; }
.btn-icon {
  background: none; border: none; cursor: pointer; font-size: 16px;
  padding: 4px 6px; border-radius: 6px; transition: background .15s;
}
.btn-icon:hover { background: var(--bg); }

/* Edit form */
.edit-form {
  padding: 16px; overflow-y: auto; flex: 1;
  display: flex; flex-direction: column; gap: 14px;
}
.form-row { display: flex; flex-direction: column; gap: 5px; }
.form-row label { font-size: 12px; font-weight: 600; color: var(--text-sub); }
.form-row input, .form-row select {
  border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 12px; font-size: 14px; width: 100%; background: var(--card);
}
.form-row input:focus, .form-row select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.12);
}
.edit-actions {
  display: flex; gap: 10px; padding-top: 4px;
  border-top: 1px solid var(--border);
}
.edit-actions .btn { flex: 1; justify-content: center; padding: 12px; }

/* Clickable cell */
.cell-link { cursor: pointer; }
.cell-link:hover { background: rgba(59,130,246,.08) !important; }

/* ===== RESPONSIVE ===== */
@media (min-width: 640px) {
  .bottom-nav { display: none; }
  body { padding-bottom: 0; }
  .main { padding: 20px; }
  .kpi-value { font-size: 22px; }
  .modal-sheet { border-radius: 16px; margin: auto; max-height: 80vh; }
  .modal-overlay { align-items: center; padding: 20px; }
}
@media (max-width: 480px) {
  .net-card-amount { font-size: 30px; }
  .summary-value   { font-size: 13px; }
  .summary-item    { padding: 12px 6px; }
  .breakdown-chart-wrap { grid-template-columns: 1fr; }
  .donut-wrap { width: 180px; height: 180px; max-width: 180px; margin: 0 auto; }
  #expense-breakdown { padding-left: 0; }
  .annual-hero-amount { font-size: 28px; }
  .annual-hero-row { gap: 6px; }
  .annual-hero-item { font-size: 10px; padding: 8px 4px; }
  .annual-hero-item-val { font-size: 12px; }
  .annual-month-grid { grid-template-columns: repeat(2, 1fr); }
}
