/*
 * 配色の方針(MyToDoと同じ)
 *  - 既定をダークにする(常用がダークのため、環境の判定に依存させない)
 *  - 背景・面・境界はすべて無彩色。純黒(#000)は使わず #121212 を基準にし、
 *    面が上に重なるほど明度を上げて階層を出す
 *  - 色を持つのはアクセント(寒色)のみ
 *  - 金額は3桁区切り・等幅数字・右揃え(DESIGN.md §8.4)
 *
 * 形・タイポグラフィのトークンは配色トークンとは独立して定義する(値の変更は自由)。
 */

:root {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface-raised: #262626;
  --line: #333333;
  --text: #e8e8e8;
  --muted: #a0a0a0;
  --accent: #6ea8fe;
  --on-accent: #10151d;
  --alert: #ff8a80;
  --income: #7fd88f;

  --tap: 44px;
  --r-sm: 4px;
  --r-md: 6px;
  --r-sheet: 14px;

  --nav-h: 56px;
  --record-h: 52px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #fafafa;
    --surface: #ffffff;
    --surface-raised: #ffffff;
    --line: #e0e0e0;
    --text: #1a1a1a;
    --muted: #5f5f5f;
    --accent: #1863dc;
    --on-accent: #ffffff;
    --alert: #c62828;
    --income: #2e7d32;
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

h1,
h2,
h3,
.label-muted,
.section-heading,
.field-label {
  font-feature-settings: "palt";
}

/* --- 共通の部品 ------------------------------------------------------------- */

input,
button,
select {
  font: inherit;
  border-radius: var(--r-sm);
}

input,
select {
  width: 100%;
  min-height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
}

input::placeholder {
  color: var(--muted);
}

label.field-set-label {
  display: block;
  margin: 0 0 4px;
  font-size: 0.8rem;
  color: var(--muted);
}

button {
  min-height: var(--tap);
  padding: 0 20px;
  border: none;
  background: none;
  color: inherit;
  font-weight: 400;
  cursor: pointer;
}

.btn-primary {
  border-radius: var(--r-md);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
}

.btn-text {
  padding: 0 4px;
  min-height: var(--tap);
  color: var(--muted);
  font-weight: 400;
}

.btn-text.accent {
  color: var(--accent);
}

.btn-text.alert {
  color: var(--alert);
}

.link-btn {
  padding: 0;
  min-height: var(--tap);
  color: var(--accent);
  font-size: 0.85rem;
}

.link-btn-block {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0 4px;
  min-height: var(--tap);
  color: var(--accent);
  font-weight: 600;
}

.link-btn-alert {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0 4px;
  min-height: var(--tap);
  color: var(--alert);
  font-weight: 600;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.error {
  margin: 6px 0 0;
  color: var(--alert);
  font-size: 0.85rem;
}

.empty-hint {
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 16px 8px;
}

/* 金額(数字span+円span)。単位は数字より一回り小さく、常に右揃え・等幅数字 */
.amount {
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
  letter-spacing: -0.01em;
}

.amount .yen-unit {
  font-size: 0.75em;
  letter-spacing: normal;
  color: var(--muted);
  margin-left: 0.05em;
}

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

/* --- ログイン ---------------------------------------------------------------- */

#login {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 24px;
}

.login-inner {
  width: min(360px, 100%);
  display: grid;
  gap: 16px;
}

.login-inner h1 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.15;
  text-align: center;
}

#login-form {
  display: grid;
  gap: 10px;
}

#login-form button[type="submit"] {
  border-radius: var(--r-md);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
}

/* --- 本体のレイアウト --------------------------------------------------------- */

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 640px;
  margin-inline: auto;
}

header {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: max(10px, env(safe-area-inset-top)) 16px 8px;
  background: var(--bg);
}

#header-date {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.conn-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--surface);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--alert);
  font-size: 0.85rem;
}

main {
  flex: 1;
  padding: 4px 16px calc(var(--nav-h) + var(--record-h) + 40px + env(safe-area-inset-bottom));
}

/* --- セクションの見出し(枠なし) ------------------------------------------------- */

.section {
  margin-top: 32px;
}

.section:first-child {
  margin-top: 8px;
}

.section-heading {
  margin: 0 0 6px;
  padding-bottom: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.02em;
  border-bottom: 1px solid var(--line);
}

.section-heading-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 6px;
  margin-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.section-heading-row .section-heading {
  margin: 0;
  padding-bottom: 0;
  border-bottom: none;
}

/* --- ホーム: 主役(今月の支出) --------------------------------------------------- */

.section-primary {
  margin-top: 8px;
}

.label-muted {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.primary-amount {
  margin: 2px 0 0;
  font-size: 2.5rem;
  font-weight: 700;
  line-height: 1.15;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
}

.secondary-line {
  margin: 4px 0 0;
  font-size: 0.9rem;
  color: var(--muted);
}

/* --- 分類別の行(棒は3px・角丸なし) ---------------------------------------------- */

.section-bars {
  margin-top: 20px;
}

.bar-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.cat-row {
  padding: 6px 0;
}

.cat-row-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 0.9rem;
}

.cat-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cat-pct {
  flex: 0 0 auto;
  width: 3em;
  text-align: right;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
}

.cat-bar-track {
  height: 3px;
  margin-top: 4px;
  background: var(--line);
}

.cat-bar-fill {
  height: 100%;
  background: var(--accent);
}

/* --- 残高 ----------------------------------------------------------------------- */

.balance-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.balance-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}

.balance-row:last-child {
  border-bottom: none;
}

.balance-left {
  min-width: 0;
}

.balance-name {
  display: block;
  overflow-wrap: anywhere;
}

.balance-kind {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.balance-right {
  flex: 0 0 auto;
  text-align: right;
}

.balance-note {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
}

/* --- 取引の行(ホーム・履歴で共通) ------------------------------------------------ */

.tx-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tx-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

.tx-row:last-child {
  border-bottom: none;
}

.tx-row:active {
  background: var(--surface-raised);
}

.tx-main {
  display: grid;
  min-width: 0;
}

.tx-label {
  overflow-wrap: anywhere;
}

.tx-sub {
  font-size: 0.8rem;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.tx-amount {
  flex: 0 0 auto;
}

/* --- 空状態 ----------------------------------------------------------------------- */

.empty-state {
  margin-top: 24px;
  display: grid;
  gap: 10px;
}

.empty-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.empty-desc {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.empty-state .btn-primary {
  margin-top: 6px;
  width: 100%;
  height: var(--record-h);
}

/* --- 履歴 ---------------------------------------------------------------------- */

.month-switch {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.icon-btn {
  width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 1.2rem;
  color: var(--text);
}

.month-label {
  min-width: 8em;
  text-align: center;
  font-weight: 700;
}

.month-totals {
  margin: 4px 0 16px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.month-totals .amount {
  display: inline;
}

.filter-row {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.filter-row select {
  flex: 1;
  min-width: 0;
}

.history-group {
  margin-bottom: 4px;
}

.history-group-heading {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  font-size: 0.85rem;
  color: var(--muted);
}

.history-group-heading .amount {
  font-size: 0.85rem;
  color: var(--text);
}

/* --- 設定 ---------------------------------------------------------------------- */

.settings-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

.settings-row:last-child {
  border-bottom: none;
}

.settings-row.archived {
  opacity: 0.55;
}

.settings-left {
  min-width: 0;
}

.settings-name {
  display: block;
  overflow-wrap: anywhere;
}

.settings-sub {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.credit-fields {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  font-size: 0.9rem;
}

.checkbox-row input {
  width: auto;
  min-height: 0;
}

.settings-logout-section {
  margin-top: 40px;
}

/* --- 下部固定: 記録するボタン・ナビゲーション ------------------------------------- */

#open-record {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom));
  z-index: 4;
  height: var(--record-h);
  max-width: 608px;
  margin-inline: auto;
}

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 4;
  display: flex;
  max-width: 640px;
  margin-inline: auto;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.nav-btn {
  flex: 1;
  min-height: var(--nav-h);
  border-top: 2px solid transparent;
  color: var(--muted);
  font-weight: 400;
  font-size: 0.85rem;
}

.nav-btn[aria-selected="true"] {
  color: var(--text);
  font-weight: 700;
  border-top-color: var(--accent);
}

/* --- 下から出るシート ------------------------------------------------------------ */

.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
}

.sheet {
  width: min(560px, 100%);
  max-height: 92dvh;
  overflow-y: auto;
  display: grid;
  gap: 14px;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--surface-raised);
  border-radius: var(--r-sheet) var(--r-sheet) 0 0;
  transform: translateY(100%);
  transition: transform 200ms ease-out;
}

.sheet-overlay.open .sheet {
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .sheet {
    transition: none;
  }
}

.sheet h2 {
  margin: 4px 0 0;
  font-size: 1rem;
}

.sheet-handle {
  width: 40px;
  height: 4px;
  margin: 6px auto 2px;
  border-radius: 999px;
  background: var(--line);
}

/* 種類の切り替え(支出/収入/振替): 区切り付きセグメント */
.segmented {
  display: flex;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  overflow: hidden;
}

.segmented-item {
  flex: 1;
  min-height: 44px;
  border-right: 1px solid var(--line);
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font-weight: 400;
}

.segmented-item:last-child {
  border-right: none;
}

.segmented-item[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  border-bottom-color: var(--accent);
}

/* 金額欄: 下線のみ・右寄せ・大きい数字。円は入力欄の外の固定表示 */
.amount-field {
  display: flex;
  align-items: baseline;
  gap: 4px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.amount-field:focus-within {
  border-bottom-color: var(--accent);
}

#record-amount {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  padding: 0;
  min-height: 0;
  height: auto;
  font-size: 2.25rem;
  font-weight: 700;
  text-align: right;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

#record-amount:focus-visible {
  outline: none;
}

.amount-unit {
  flex: 0 0 auto;
  font-size: 1.1rem;
  color: var(--muted);
}

.field-group {
  display: grid;
  gap: 6px;
}

.field-label {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.chip-row.chip-row-scroll {
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding-bottom: 2px;
}

.chip-row.chip-row-scroll::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: 0 0 auto;
  min-height: 44px;
  max-width: 12rem;
  padding: 0 14px;
  border: 2px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--text);
  font-weight: 700;
}

.chip-row input[type="date"] {
  flex: 1 1 auto;
  min-width: 140px;
  min-height: 44px;
}

.sheet-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.sheet-actions .btn-text.alert {
  margin-right: auto;
}

.sheet-actions .btn-primary {
  flex: 1;
}

/* --- トースト ------------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--record-h) + 24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: calc(100% - 32px);
  white-space: nowrap;
  padding: 10px 16px;
  background: var(--surface-raised);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

.toast button {
  min-height: 32px;
  padding: 0 14px;
  color: var(--accent);
  font-weight: 600;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
