/* ============================================================
   FXノート — テーマトークン
   配色は dataviz リファレンスパレット準拠 (検証済み)。
   プラス=青 / マイナス=赤 (「赤字」)。テキストは常にインクトークン。
   ============================================================ */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --pos: #2a78d6;          /* 利益 */
  --neg: #e34948;          /* 損失 */
  --neutral-mid: #f0efec;  /* ±0 / 中立 */
  --focus-ring: #2a78d6;
  --danger-text: #b93534;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 2px 8px rgba(11, 11, 11, 0.04);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --pos: #3987e5;
  --neg: #e66767;
  --neutral-mid: #383835;
  --focus-ring: #3987e5;
  --danger-text: #e66767;
  --shadow: none;
}

/* ============ ベース ============ */

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

/* スマホ回転時の勝手な文字拡大を防ぐ */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo, sans-serif;
  font-size: 15px;
  line-height: 1.6;
}

/* ノッチ等のセーフエリアを避ける (PWAスタンドアロン・横向き対応) */
.wrap {
  max-width: 1040px;
  margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

h1, h2, h3 { font-weight: 600; line-height: 1.4; }
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 15px; margin: 0 0 8px; color: var(--text-secondary); }

button { font: inherit; touch-action: manipulation; }

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ============ ヘッダー / フッター ============ */

.site-header {
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  padding-top: env(safe-area-inset-top);
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark { display: block; }

.site-footer {
  margin-top: 40px;
  padding: 20px 0 calc(32px + env(safe-area-inset-bottom));
  color: var(--text-muted);
  font-size: 12.5px;
  text-align: center;
}

/* ============ カード / セクション ============ */

main.wrap { padding-top: 20px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin: 24px 0 12px;
}
.section-head h2 { margin: 0; }

/* ============ ボタン ============ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-1);
  color: var(--text-primary);
  cursor: pointer;
}
.btn:hover { border-color: var(--baseline); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:disabled:hover { border-color: var(--border); }

.btn-primary {
  background: var(--pos);
  border-color: var(--pos);
  color: #fff;
  font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.08); border-color: var(--pos); }

.btn-ghost { border-color: transparent; background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { border-color: var(--border); }

.btn-small { padding: 3px 10px; font-size: 13px; border-radius: 6px; }

.btn-danger { color: var(--danger-text); }
.btn-danger:hover { border-color: var(--danger-text); }

.link-btn {
  border: none;
  background: none;
  padding: 0;
  color: var(--pos);
  text-decoration: underline;
  cursor: pointer;
}

/* ============ テーマ切替 (ライト/ダーク/自動) ============ */

.theme-switch {
  display: inline-flex;
  align-items: center;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--page);
}

.theme-opt {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12.5px;
  line-height: 1;
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.theme-opt:hover { color: var(--text-primary); }
.theme-opt[aria-pressed="true"] {
  background: var(--surface-1);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: 0 0 0 1px var(--border);
}

/* ============ 入力フォーム ============ */

.entry-grid {
  border: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 160px 160px 1fr auto;
  gap: 12px;
  align-items: end;
  min-width: 0; /* fieldsetのUA既定 min-width:min-content を打ち消し、カード幅に収める */
}

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 12.5px; color: var(--text-secondary); }
.optional { color: var(--text-muted); font-weight: 400; }

.field input {
  padding: 7px 10px;
  border: 1px solid var(--baseline);
  border-radius: 8px;
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 16px; /* 16px未満だとiOSがフォーカス時に自動ズームする */
  min-width: 0;
}
.field input:focus-visible { outline-offset: 0; }

#f-pnl { font-variant-numeric: tabular-nums; }

.entry-hint { margin: 8px 0 0; font-size: 12.5px; color: var(--text-secondary); }

fieldset:disabled .btn, fieldset:disabled input { opacity: 0.55; cursor: not-allowed; }

/* ============ タブ切替 ============ */

.tabs {
  display: flex;
  gap: 4px;
  margin: 24px 0 16px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab-btn {
  flex: none;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  cursor: pointer;
  white-space: nowrap;
}
.tab-btn:hover { color: var(--text-primary); }
.tab-btn[aria-selected="true"] {
  color: var(--text-primary);
  border-bottom-color: var(--pos);
}

.tab-panel[hidden] { display: none; }
.tab-panel > .section-head { margin-top: 0; }

/* ============ 期間フィルター ============ */

.filter-row { display: flex; gap: 4px; flex-wrap: wrap; }

.range-btn {
  padding: 5px 12px;
  font-size: 13px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}
.range-btn:hover { background: var(--neutral-mid); }
.range-btn.is-active {
  background: var(--surface-1);
  border-color: var(--baseline);
  color: var(--text-primary);
  font-weight: 600;
}

/* ============ KPIタイル ============ */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}

.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.stat-label { font-size: 12.5px; color: var(--text-secondary); }

.stat-value {
  font-size: 24px;
  font-weight: 600;
  line-height: 1.3;
  display: flex;
  align-items: baseline;
  gap: 6px;
  white-space: nowrap;
}

.stat-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* 値そのものはインク色。符号マーカー(▲▼)だけが色を持つ */
.delta-mark { font-size: 13px; }
.delta-mark.pos { color: var(--pos); }
.delta-mark.neg { color: var(--neg); }

/* ============ チャート ============ */

.chart { width: 100%; position: relative; }
.chart svg { display: block; width: 100%; height: auto; }

.chart-empty {
  padding: 48px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13.5px;
}

.chart .axis-text {
  fill: var(--text-muted);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.chart .direct-label {
  fill: var(--text-primary);
  font-size: 12px;
  font-weight: 600;
}
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .zeroline { stroke: var(--baseline); stroke-width: 1; }
.chart .crosshair { stroke: var(--baseline); stroke-width: 1; }

.chart .bar { cursor: default; }
.chart .bar:hover, .chart .bar:focus-visible { filter: brightness(1.12); }
.chart .bar:focus-visible { outline: none; stroke: var(--focus-ring); stroke-width: 1.5; }

.chart .hover-overlay { cursor: crosshair; }
.chart .hover-overlay:focus-visible { outline: none; }
.chart svg:has(.hover-overlay:focus-visible) { outline: 2px solid var(--focus-ring); outline-offset: 2px; border-radius: 4px; }

/* ============ カレンダー ============ */

.calendar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}
.calendar-head h2 { margin: 0; }

.calendar-nav { display: flex; align-items: center; gap: 4px; }
.calendar-title {
  min-width: 110px;
  text-align: center;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.cal-dow {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-muted);
  padding: 2px 0;
}

.cal-cell {
  min-height: 58px;
  border-radius: 8px;
  padding: 4px 6px;
  background: var(--page);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}
.cal-cell.is-blank { background: transparent; }
.cal-cell.has-data { cursor: default; }
.cal-cell.is-today { box-shadow: inset 0 0 0 2px var(--baseline); }

.cal-day { font-size: 11px; color: var(--text-muted); line-height: 1; }
.cal-val {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  text-align: right;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.cal-memo-dot {
  position: absolute;
  top: 5px;
  right: 6px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--text-muted);
}

.calendar-note { margin: 10px 0 0; font-size: 12px; color: var(--text-muted); }

/* ============ 記録テーブル ============ */

.table-scroll { overflow-x: auto; max-height: 420px; overflow-y: auto; }

#records-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }

#records-table th {
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--baseline);
  padding: 6px 10px;
  position: sticky;
  top: 0;
  background: var(--surface-1);
}

#records-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--grid);
  vertical-align: top;
}

#records-table .num { text-align: right; }
#records-table td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
#records-table .ops { text-align: right; white-space: nowrap; }
#records-table td.date-cell { white-space: nowrap; font-variant-numeric: tabular-nums; }
#records-table td.memo-cell { color: var(--text-secondary); max-width: 420px; }

.empty-note { color: var(--text-muted); font-size: 13.5px; margin: 8px 0 0; }

/* ============ データ管理 ============ */

.data-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.data-note { margin: 12px 0 0; font-size: 12.5px; color: var(--text-muted); }

/* ============ デモバナー / ツールチップ / トースト ============ */

.demo-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap; /* スマホ幅で横はみ出しさせない */
  gap: 8px 12px;
  background: var(--neutral-mid);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  margin-bottom: 16px;
  font-size: 13.5px;
}

.tooltip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
  padding: 8px 12px;
  font-size: 12.5px;
  max-width: 280px;
}

.tooltip .tt-title { color: var(--text-secondary); font-size: 12px; margin-bottom: 2px; }

.tooltip .tt-row { display: flex; align-items: baseline; gap: 8px; }
.tooltip .tt-key {
  width: 12px;
  height: 0;
  border-top: 2px solid transparent;
  flex: none;
  align-self: center;
}
.tooltip .tt-value { font-weight: 600; font-variant-numeric: tabular-nums; }
.tooltip .tt-label { color: var(--text-secondary); font-size: 12px; }
.tooltip .tt-memo { color: var(--text-secondary); font-size: 12px; margin-top: 4px; overflow-wrap: anywhere; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  background: var(--text-primary);
  color: var(--page);
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 13.5px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}

/* ============ レスポンシブ ============ */

@media (max-width: 760px) {
  .entry-grid { grid-template-columns: 1fr 1fr; }
  .field-memo { grid-column: 1 / -1; }
  .field-submit { grid-column: 1 / -1; }
  .field-submit .btn { width: 100%; }

  .kpi-row { grid-template-columns: 1fr 1fr; }
  .stat-value { font-size: 18px; white-space: normal; overflow-wrap: anywhere; }

  .cal-cell { min-height: 46px; padding: 3px 4px; }
  .cal-val { font-size: 10px; }
}

/* スマホ: 縦積み + 横スクロールフィルター */
@media (max-width: 640px) {
  .card { padding: 14px; }

  .section-head { flex-direction: column; align-items: stretch; }
  .filter-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .filter-row::-webkit-scrollbar { display: none; }
  .range-btn { flex: none; white-space: nowrap; }

  .data-actions { display: grid; grid-template-columns: 1fr 1fr; }

  #records-table th, #records-table td { padding: 6px 8px; }
  /* メモ列は潰さず、テーブル自体を横スクロールさせる */
  #records-table td.memo-cell { min-width: 160px; max-width: 220px; }

  .calendar-head { flex-direction: column; align-items: stretch; }
  .calendar-nav { justify-content: space-between; }
}

/* タッチ端末: 十分なタッチターゲットを確保 */
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .btn-small { min-height: 36px; }
  .range-btn { min-height: 36px; padding: 6px 14px; }
  .tab-btn { min-height: 44px; }
  .theme-opt { min-height: 32px; }
  .field input { min-height: 44px; }
  .cal-cell { min-height: 52px; }
}

/* 極小画面 */
@media (max-width: 380px) {
  h1 { font-size: 16px; }
  .theme-opt { padding: 4px 8px; font-size: 12px; }
  .wrap {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
}
