/*
 * ローカル会計 共通スタイル
 * スマホファースト：基本はスマートフォン。広い画面は min-width で足す（設計書 画面 1. レスポンシブ方針）
 *   〜599px  スマートフォン（基本）
 *   600px〜  タブレット
 *   1024px〜 PC（サイドメニュー + 本文）
 */

:root {
  --c-bg: #f4f6f9;
  --c-surface: #ffffff;
  --c-text: #1c2430;
  --c-muted: #5c6878;
  --c-border: #d9dfe7;
  --c-primary: #1f5fbf;
  --c-primary-weak: #e6eefb;
  --c-brand: #1f3a5f;
  --c-income: #0e7a46;
  --c-expense: #b4421a;
  --c-danger: #b42318;
  --c-danger-weak: #fdecea;
  --c-focus: #f59e0b;

  --radius: 12px;
  --radius-s: 8px;
  --gap: 16px;
  --tap: 44px;
  --appbar-h: 56px;
  --tabbar-h: 60px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 10%);

  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--c-text);
  background: var(--c-bg);
  -webkit-text-size-adjust: 100%;
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-height: 100dvh; overflow-x: hidden; }
h1, h2, h3, p { margin: 0; }
a { color: var(--c-primary); }
img { max-width: 100%; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--c-focus); outline-offset: 2px; }

.icon { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- レイアウト ---------- */

.sidenav { display: none; }

.main { min-width: 0; }

.appbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  min-height: var(--appbar-h);
  padding: 0 var(--gap);
  background: var(--c-brand); color: #fff;
}
.appbar__title { font-size: 1.125rem; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.appbar__back { display: inline-flex; align-items: center; justify-content: center; width: var(--tap); height: var(--tap); margin-left: -12px; color: inherit; }
.appbar__actions { display: flex; gap: 4px; }
.appbar__actions .btn { color: #fff; border-color: rgb(255 255 255 / 50%); background: transparent; min-height: 36px; }

.content {
  padding: var(--gap);
  /* タブバーと下端固定ボタンの分を空ける */
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 96px);
  display: grid; gap: var(--gap);
  align-content: start;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--c-surface);
  border-top: 1px solid var(--c-border);
}
.tabbar a, .tabbar button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: var(--tap); min-height: var(--tap);
  border: 0; background: none; font: inherit; font-size: .6875rem;
  color: var(--c-muted); text-decoration: none; cursor: pointer;
}
.tabbar [aria-current="page"] { color: var(--c-primary); font-weight: 700; }
.tabbar__add .icon { width: 32px; height: 32px; padding: 4px; border-radius: 50%; background: var(--c-primary); color: #fff; }

/* ---------- シート（下から出る選択肢） ---------- */

.sheet {
  width: 100%; max-width: 560px;
  margin: auto auto 0; padding: var(--gap) var(--gap) calc(var(--gap) + var(--safe-bottom));
  border: 0; border-radius: var(--radius) var(--radius) 0 0;
  background: var(--c-surface); color: var(--c-text);
}
.sheet[open] { display: grid; gap: 12px; }
.sheet::backdrop { background: rgb(0 0 0 / 40%); }
.sheet__title { font-size: 1rem; text-align: center; color: var(--c-muted); }

/* ---------- 部品 ---------- */

.card {
  background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: var(--gap); display: grid; gap: 12px; min-width: 0;
}
.card__title { font-size: 1rem; font-weight: 700; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 16px;
  border: 1px solid var(--c-border); border-radius: var(--radius-s);
  background: var(--c-surface); color: var(--c-text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn--primary { background: var(--c-primary); border-color: var(--c-primary); color: #fff; }
.btn--danger { background: var(--c-surface); border-color: var(--c-danger); color: var(--c-danger); }
.btn--ghost { border-color: transparent; background: transparent; color: var(--c-muted); }
.btn--block { display: flex; width: 100%; }
.btn--large { min-height: 56px; font-size: 1.0625rem; }
.btn--small { min-height: var(--tap); padding: 0 10px; font-size: .875rem; }
.btn--icon { width: var(--tap); padding: 0; }

.fab {
  position: fixed; right: var(--gap); bottom: calc(var(--tabbar-h) + var(--safe-bottom) + var(--gap)); z-index: 15;
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--c-primary); color: #fff; box-shadow: 0 4px 12px rgb(0 0 0 / 25%);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}

/* タブ（入力方法の切り替え） */
.tabs { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; border-radius: var(--radius-s); background: #e4e9f0; }
.tabs [role="tab"] {
  min-height: var(--tap); border: 0; border-radius: 6px; background: transparent;
  font: inherit; font-size: .9375rem; color: var(--c-muted); cursor: pointer;
}
.tabs [role="tab"][aria-selected="true"] { background: var(--c-surface); color: var(--c-text); font-weight: 700; box-shadow: var(--shadow); }

/* フォーム：スマートフォンは項目名を上、入力欄を幅いっぱいに */
.form { display: grid; gap: 16px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field > label, .field__label { font-size: .875rem; font-weight: 600; color: var(--c-muted); }
.field__row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.input, .select, .textarea {
  width: 100%; min-height: var(--tap); padding: 8px 12px;
  border: 1px solid var(--c-border); border-radius: var(--radius-s);
  background: var(--c-surface); color: var(--c-text);
  font: inherit; font-size: 16px; /* 16px 未満だと iOS で拡大される */
}
.textarea { min-height: 88px; resize: vertical; }
.input--amount { font-size: 1.5rem; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.input-unit { position: relative; }
.input-unit .input { padding-right: 2.5em; }
.input-unit::after { content: "円"; position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--c-muted); }
.check { display: flex; align-items: center; gap: 8px; min-height: var(--tap); }
.check input { width: 22px; height: 22px; }

.field--error .input, .field--error .select, .field--error .textarea { border-color: var(--c-danger); background: var(--c-danger-weak); }
.field__error { color: var(--c-danger); font-size: .875rem; }
.form-error { color: var(--c-danger); background: var(--c-danger-weak); border-radius: var(--radius-s); padding: 8px 12px; }

/* 登録ボタン：スマートフォンでは画面下（タブバーの上）に固定 */
.submit-bar {
  position: fixed; left: 0; right: 0; z-index: 15;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  padding: 8px var(--gap);
  background: rgb(244 246 249 / 95%); border-top: 1px solid var(--c-border);
}
.form-actions { display: grid; gap: 8px; }

/* レシート */
.receipt-picker {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 96px; border: 2px dashed var(--c-border); border-radius: var(--radius);
  background: var(--c-surface); color: var(--c-muted); cursor: pointer; text-align: center;
}
.receipt-picker input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.receipt-picker:focus-within { outline: 3px solid var(--c-focus); }
.receipt-thumb { display: block; max-height: 200px; margin: 0 auto; border-radius: var(--radius-s); cursor: zoom-in; }
.viewer { width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; padding: 0; border: 0; background: #000; }
.viewer[open] { display: flex; align-items: center; justify-content: center; }
.viewer img { max-width: 100%; max-height: 100%; touch-action: pinch-zoom; }
.viewer button { position: fixed; top: 8px; right: 8px; }

/* サマリ */
.summary { display: grid; gap: 8px; }
.summary__row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.summary__label { color: var(--c-muted); }
.summary__value { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.summary__row--balance { border-top: 1px solid var(--c-border); padding-top: 8px; }
.summary__row--balance .summary__value { font-size: 1.5rem; }
.amount { font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount--income { color: var(--c-income); }
.amount--expense { color: var(--c-expense); }
.amount--minus { color: var(--c-danger); }

/* 月の切り替え */
.month-nav { display: flex; align-items: center; gap: 4px; }
.month-nav__label { flex: 1; text-align: center; font-weight: 700; font-size: 1.0625rem; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar > .month-nav { flex: 1; }

/* 一覧：スマートフォンはカード、PC は表 */
.entry-cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.entry-card {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px;
  padding: 12px var(--gap); min-height: var(--tap);
  background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow);
  color: inherit; text-decoration: none;
}
.entry-card__date { grid-column: 1 / -1; font-size: .8125rem; color: var(--c-muted); }
.entry-card__title { grid-row: 2; grid-column: 1; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-card__amount { grid-row: 2; grid-column: 2; font-weight: 700; }
.entry-card__meta { grid-column: 1 / -1; font-size: .8125rem; color: var(--c-muted); }
.entry-table-wrap { display: none; }
.empty { color: var(--c-muted); text-align: center; padding: 24px 0; }

.table { width: 100%; border-collapse: collapse; background: var(--c-surface); }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--c-border); text-align: left; }
.table th { font-size: .8125rem; color: var(--c-muted); font-weight: 600; background: #f8fafc; }
.table td.num, .table th.num { text-align: right; }
.table tbody tr[data-href] { cursor: pointer; }
.table tbody tr[data-href]:hover { background: var(--c-primary-weak); }
.table-scroll { overflow-x: auto; border-radius: var(--radius); box-shadow: var(--shadow); }

.badge { display: inline-block; padding: 0 8px; border-radius: 999px; font-size: .75rem; background: #e4e9f0; color: var(--c-muted); }
.badge--income { background: #e3f4ea; color: var(--c-income); }
.badge--expense { background: #fbeae3; color: var(--c-expense); }

/* セグメント（集計の切り替え） */
.segment { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: var(--radius-s); background: #e4e9f0; }
.segment a { display: flex; align-items: center; justify-content: center; min-height: var(--tap); border-radius: 6px; color: var(--c-muted); text-decoration: none; }
.segment a[aria-current="page"] { background: var(--c-surface); color: var(--c-text); font-weight: 700; box-shadow: var(--shadow); }

/* グラフ */
.chart-scroll { overflow-x: auto; overscroll-behavior-x: contain; }
.chart { display: block; height: 220px; }
.chart text { font-size: 11px; fill: var(--c-muted); }
.chart .bar--income { fill: var(--c-income); }
.chart .bar--expense { fill: var(--c-expense); }
.chart .axis { stroke: var(--c-border); }
.legend { display: flex; gap: 16px; font-size: .8125rem; color: var(--c-muted); }
.legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 4px; border-radius: 2px; background: currentColor; vertical-align: middle; }
.legend .legend--income { color: var(--c-income); }
.legend .legend--expense { color: var(--c-expense); }

/* カテゴリ別：横棒 */
.cat-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.cat-item { background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.cat-item summary { list-style: none; cursor: pointer; padding: 12px var(--gap); display: grid; gap: 6px; min-height: var(--tap); }
.cat-item summary::-webkit-details-marker { display: none; }
.cat-row { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.cat-row__name { font-weight: 700; }
.cat-row__ratio { color: var(--c-muted); font-size: .8125rem; margin-left: 6px; }
.hbar { height: 8px; border-radius: 4px; background: #e4e9f0; overflow: hidden; }
.hbar > span { display: block; height: 100%; background: var(--c-expense); border-radius: 4px; }
.cat-children { list-style: none; margin: 0; padding: 0 var(--gap) 12px; display: grid; gap: 4px; }
.cat-children a { display: grid; gap: 4px; padding: 8px 0; color: inherit; text-decoration: none; min-height: var(--tap); border-top: 1px solid var(--c-border); }
.cat-children .hbar > span { background: #e08a62; }

/* 管理画面の一覧 */
.item-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  min-height: var(--tap); padding: 10px var(--gap);
  background: var(--c-surface); border: 0; border-radius: var(--radius); box-shadow: var(--shadow);
  font: inherit; color: inherit; text-align: left; cursor: pointer;
}
.item__main { flex: 1; min-width: 0; display: grid; }
.item__name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item__sub { font-size: .8125rem; color: var(--c-muted); }
.item--inactive { opacity: .6; }

.tree { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.tree-major { background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 8px; display: grid; gap: 4px; }
.tree-row { display: flex; align-items: center; gap: 4px; min-height: var(--tap); }
.tree-row__name { flex: 1; min-width: 0; padding-left: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-row--major .tree-row__name { font-weight: 700; }
.tree-children { list-style: none; margin: 0 0 0 12px; padding: 0 0 0 8px; border-left: 2px solid var(--c-border); display: grid; gap: 2px; }
.tree-add { display: flex; gap: 8px; padding: 4px 0 4px 20px; }
.tree-add .input { flex: 1; min-width: 0; }

/* モーダル（編集フォーム） */
.modal {
  width: min(100% - 16px, 560px); max-height: calc(100dvh - 32px); overflow: auto;
  padding: var(--gap); border: 0; border-radius: var(--radius);
  background: var(--c-surface); color: var(--c-text);
}
.modal::backdrop { background: rgb(0 0 0 / 40%); }
.modal__title { font-size: 1.125rem; margin-bottom: 12px; }

/* 通知 */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 50;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 84px);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: var(--radius-s);
  background: #1c2430; color: #fff; box-shadow: 0 4px 12px rgb(0 0 0 / 25%);
}
.toast--error { background: var(--c-danger); }

/* ---------- タブレット（600px〜） ---------- */
@media (min-width: 600px) {
  .content { max-width: 640px; margin: 0 auto; padding-left: 24px; padding-right: 24px; }
  .submit-bar { left: 50%; transform: translateX(-50%); width: 100%; max-width: 640px; border: 0; background: transparent; }
}

/* ---------- PC（1024px〜） ---------- */
@media (min-width: 1024px) {
  :root { --tabbar-h: 0px; }

  .layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100dvh; }
  .sidenav {
    display: block; position: sticky; top: 0; height: 100dvh; overflow-y: auto;
    background: var(--c-brand); color: #fff; padding: 16px 12px;
  }
  .sidenav__brand { display: block; padding: 8px 12px 16px; color: #fff; font-weight: 700; font-size: 1.125rem; text-decoration: none; }
  .sidenav ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
  .sidenav a:not(.sidenav__brand) { display: flex; align-items: center; min-height: var(--tap); padding: 0 12px; border-radius: var(--radius-s); color: rgb(255 255 255 / 85%); text-decoration: none; }
  .sidenav a:not(.sidenav__brand):hover { background: rgb(255 255 255 / 10%); }
  .sidenav a[aria-current="page"] { background: rgb(255 255 255 / 18%); color: #fff; font-weight: 700; }

  .tabbar { display: none; }
  .appbar { background: var(--c-surface); color: var(--c-text); border-bottom: 1px solid var(--c-border); }
  .appbar__actions .btn { color: var(--c-text); border-color: var(--c-border); }

  .content { max-width: 1040px; margin: 0; padding: 24px 32px 48px; }
  .content--narrow, .content:has(> .form) { max-width: 720px; }

  /* PC のフォームは項目名を左、入力欄を右に */
  .form .field { grid-template-columns: 140px 1fr; align-items: center; column-gap: 16px; }
  .form .field > label, .form .field__label { padding-top: 0; }
  .form .field .field__error, .form .field .field__help { grid-column: 2; }
  .form .field--top { align-items: start; }

  .submit-bar { position: static; transform: none; max-width: none; padding: 0; border: 0; background: none; }
  .form-actions { grid-template-columns: auto auto; justify-content: start; }
  .fab { bottom: 32px; right: 32px; }
  .toast { bottom: 32px; }

  .entry-cards { display: none; }
  .entry-table-wrap { display: block; }

  .home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); align-items: start; }
  .report-grid { display: grid; grid-template-columns: 3fr 2fr; gap: var(--gap); align-items: start; }

  .sheet { margin: auto; border-radius: var(--radius); }
}

/* ---------- 一覧・集計・管理画面 ---------- */
.stack { display: grid; gap: var(--gap); }
.muted { color: var(--c-muted); font-size: .875rem; }
.card__head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.home-actions { display: grid; gap: 8px; }
.totals { grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 12px; }
.totals__item { display: grid; text-align: center; font-weight: 700; min-width: 0; }
.totals__item .amount { font-size: .9375rem; overflow: hidden; text-overflow: ellipsis; }
.summary__row--plain .summary__value { font-size: 1.25rem; }
.filter-summary { font-size: .875rem; color: var(--c-muted); }
.toolbar__label { display: none; }
.chart--monthly { width: 200%; } /* スマートフォンは 6 か月分を表示し、横にスワイプ */
.table--monthly tbody tr { cursor: pointer; }
.table--monthly td:first-child a { color: inherit; font-weight: 600; text-decoration: none; }
.form-actions--modal { display: flex; flex-wrap: wrap; gap: 8px; }
.form-actions--modal .btn--primary { flex: 1 0 100%; }
.form--stacked .field { grid-template-columns: 1fr !important; }
.tree-new { padding: 12px var(--gap); }
.tree-add--major { padding: 0; }

@media (min-width: 600px) {
  .chart--monthly { width: 100%; }
  .toolbar__label { display: inline; }
}

@media (min-width: 1024px) {
  .entry-cards--always { display: grid; }
  .form-actions--modal .btn--primary { flex: 0 0 auto; }
}
.entry-table-wrap.is-empty { display: none !important; }

/* 月次収支の表：スマートフォンでも 1 行に収める */
.table--monthly th, .table--monthly td { padding: 10px 8px; white-space: nowrap; font-size: .9375rem; }
@media (min-width: 600px) {
  .table--monthly th, .table--monthly td { padding: 10px 12px; font-size: 1rem; }
}

/* ---------- ログイン・ログアウト ---------- */
.guest { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px calc(24px + var(--safe-bottom)); }
.guest__main { width: 100%; max-width: 400px; display: grid; gap: 16px; }
.guest__brand { text-align: center; font-size: 1.375rem; font-weight: 700; color: var(--c-brand); }
.guest__card { padding: 24px 20px; }
.guest__card .card__title { font-size: 1.125rem; }
.sheet__user { text-align: center; }
.sidenav__user { display: grid; gap: 8px; margin-top: 24px; padding: 12px; border-top: 1px solid rgb(255 255 255 / 20%); }
.sidenav__name { color: rgb(255 255 255 / 85%); font-size: .875rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidenav .sidenav__logout { width: 100%; background: transparent; color: #fff; border-color: rgb(255 255 255 / 50%); }

/* ---------- ユーザ管理 ---------- */
.item__badges { display: flex; gap: 4px; flex: none; }
.badge--admin { background: var(--c-primary-weak); color: var(--c-primary); }

/* ---------- 設定：API キー ---------- */
.api-key { display: grid; gap: 4px; }
.api-key__row { display: flex; gap: 8px; }
.api-key__value { flex: 1; min-width: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .875rem; }
