/* 色はここにまとめる（ダークモードでは下で上書き） */
:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1f2328;
  --sub: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --done: #16a34a;
  --done-bg: #e8f7ee;
  --error: #dc2626;
  --streak: #c2410c;
  --streak-bg: #fff1e6;
  --head-bg: #e6f0ec;
  --head-line: #b9d3c8;
  /* 次にやること専用の色（青い文字・ボタンと見分けるため紫系） */
  --next: #5b21b6;
  --next-bg: #ede9fe;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px rgba(16, 24, 40, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --card: #1b2027;
    --text: #e6e8eb;
    --sub: #9aa3ae;
    --line: #2b313a;
    --accent: #60a5fa;
    --done: #4ade80;
    --done-bg: #12261a;
    --error: #f87171;
    --streak: #fdba74;
    --streak-bg: #3a2416;
    --head-bg: #18282a;
    --head-line: #2f4a47;
    --next: #c4b5fd;
    --next-bg: #2e2346;
    --shadow: 0 0 0 1px #2b313a;
  }
}

* { box-sizing: border-box; }

/* hidden を付けた要素は、ほかの display の指定より優先して必ず隠す
   （.row { display: grid } などがあると、hidden を付けても見えてしまうため） */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  /* 下のタブの高さ＋iPhoneのホームバーの分だけ余白を空ける */
  padding-bottom: calc(80px + env(safe-area-inset-bottom));
}

.top {
  padding: calc(16px + env(safe-area-inset-top)) 16px 8px;
}
.top h1 { margin: 0; font-size: 26px; }
.sub { margin: 4px 0 0; color: var(--sub); font-size: 14px; }

main { padding: 0 16px; }

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

.message { color: var(--sub); text-align: center; padding: 32px 0; }
.error { color: var(--error); font-size: 14px; min-height: 1em; margin: 8px 0 0; }

/* ---- 今日のまとめ ---- */
.summary {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  margin-bottom: 8px;
  background: var(--card);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.summary[hidden] { display: none; }
/* 輪っかのグラフ：--ratio（0〜1）の割合だけ色を塗る */
.ring {
  --ratio: 0;
  width: 64px;
  height: 64px;
  flex: none;
  border-radius: 50%;
  background: conic-gradient(var(--done) calc(var(--ratio) * 360deg), var(--line) 0);
  display: grid;
  place-items: center;
}
.ring span {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--card);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 15px;
}
.summary-label { color: var(--sub); font-size: 13px; }
.summary-text { font-size: 22px; font-weight: 700; margin-top: 2px; }

.group-title {
  font-size: 14px;
  color: var(--sub);
  font-weight: 600;
  margin: 20px 4px 8px;
}
.group-title[hidden] { display: none; }

/* ---- 横にスクロールするタブ（一覧・カテゴリ） ---- */
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  /* 画面の端までスクロールできるように、main の左右の余白の外まで広げる */
  margin: 12px -16px 0;
  padding: 2px 16px 8px;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
}
.chips[hidden] { display: none; }
.chip {
  flex: none;
  min-width: 108px;
  max-width: 200px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  padding: 10px 14px;
  background: var(--card);
  color: var(--text);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  text-align: left;
  scroll-snap-align: start;
}
.chip.active { background: var(--head-bg); border-color: var(--head-line); }
.chip-top { display: flex; align-items: center; gap: 8px; max-width: 100%; }
.chip-name { font-size: 15px; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-count {
  flex: none;
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--streak-bg);
  color: var(--streak);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}
.chip-sub { color: var(--sub); font-size: 12px; font-weight: 500; white-space: nowrap; }

/* ---- カテゴリのカード（スマホは1列、広い画面は横に並ぶ） ---- */
.cat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr)); /* 狭い画面では画面幅に合わせる */
  gap: 14px;
  margin-top: 10px;
}
.cat-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.cat-head {
  padding: 14px 16px 12px;
  background: var(--head-bg);
  border-bottom: 1px solid var(--head-line);
}
.cat-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.cat-name { font-size: 19px; font-weight: 800; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-badge {
  flex: none;
  padding: 2px 10px;
  border: 1.5px solid var(--streak);
  border-radius: 8px;
  color: var(--streak);
  font-size: 13px;
  font-weight: 700;
}
.cat-card.all-done .cat-badge { border-color: var(--done); color: var(--done); }
.mini-bar { height: 6px; margin-top: 10px; background: var(--line); border-radius: 3px; overflow: hidden; }
.mini-bar span { display: block; height: 100%; background: var(--done); }

.cat-body { padding: 4px 16px 12px; }
.cat-label { color: var(--sub); font-size: 13px; font-weight: 700; margin: 12px 0 4px; }

.todo-rows, .done-rows { list-style: none; margin: 0; padding: 0; }
.todo-rows li + li, .done-rows li + li { border-top: 1px solid var(--line); }

/* これからやること：押すと記録フォーム */
.todo-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0 8px;
  cursor: pointer;
}
.todo-row:active { opacity: 0.6; }
.dot { flex: none; width: 9px; height: 9px; border-radius: 50%; background: var(--streak); }
.row-main { flex: 1; min-width: 0; }
.row-line { display: flex; align-items: center; gap: 6px; }
.row-name { font-size: 16px; font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { display: flex; align-items: center; gap: 4px; color: var(--sub); font-size: 13px; margin-top: 1px; }
.row-left { flex: none; font-size: 20px; font-weight: 800; color: var(--text); letter-spacing: -0.3px; }
.check-circle {
  flex: none;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  background: none;
  border: 3px solid var(--done);
}
.todo-rows .bar { height: 6px; margin: 0 0 10px 19px; background: var(--line); border-radius: 3px; overflow: hidden; }
.todo-rows .bar span { display: block; height: 100%; background: var(--done); border-radius: 3px; }

.badge {
  flex: none;
  font-size: 12px;
  font-weight: 600;
  color: var(--streak);
  background: var(--streak-bg);
  padding: 2px 7px;
  border-radius: 999px;
}

/* 達成済み：1行に小さく、色は控えめ */
.done-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  cursor: pointer;
}
.done-row .row-name { flex: 1; font-size: 15px; font-weight: 600; color: var(--sub); }
.done-row .result { flex: none; color: var(--sub); font-size: 13px; }
.done-mark {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--done);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
}

/* ---- 習慣タブのカテゴリ見出し ---- */
.group { margin-top: 20px; }
.group-head { display: flex; align-items: center; gap: 10px; margin: 0 4px 10px; }
.group-head .group-name { font-size: 18px; font-weight: 800; }
.group-head .group-count { color: var(--sub); font-size: 14px; font-weight: 600; }

/* ---- 記録フォーム ---- */
dialog {
  width: 100%;
  max-width: 480px;
  margin: auto auto 0;
  border: none;
  border-radius: 18px 18px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  background: var(--card);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }

label { display: block; font-size: 14px; color: var(--sub); margin-top: 12px; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row label { margin-top: 0; }

textarea { resize: vertical; line-height: 1.5; font-family: inherit; }

input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 17px; /* 16px未満だとiPhoneが勝手に拡大する */
}

.buttons { display: flex; gap: 12px; margin-top: 16px; }
.buttons button { flex: 1; }

button {
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}
button.secondary { background: var(--line); color: var(--text); }
button:disabled { opacity: 0.5; }

/* ---- 習慣タブ ---- */
button.wide { width: 100%; margin-bottom: 12px; }

.rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.rows li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 12px 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
}
/* ボタンが入りきらないときは、次の行に回す */
.rows li { flex-wrap: wrap; }
.rows .main { flex: 1 1 150px; min-width: 0; }
.rows .name { font-weight: 600; }
.rows .detail { color: var(--sub); font-size: 13px; margin-top: 2px; }
.rows button { padding: 8px 12px; font-size: 14px; }

.archived { margin-top: 24px; }
.archived summary { color: var(--sub); padding: 8px 0; cursor: pointer; }
.archived .name { color: var(--sub); }

/* ---- カードの「履歴」ボタン ---- */
.history-link {
  flex: none;
  background: none;
  color: var(--sub);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 600;
}
.row-sub .history-link { margin-left: 4px; }

/* 記録フォームが長くなったので、画面からはみ出たらスクロール */
#log-dialog { max-height: 92vh; overflow-y: auto; }

/* ---- 履歴画面（全画面） ---- */
dialog.fullscreen {
  max-width: none;
  max-height: none;
  width: 100%;
  height: 100%;
  margin: 0;
  border-radius: 0;
  background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
  overflow-y: auto;
}
.history-top { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.history-top button { padding: 10px 12px; font-size: 14px; flex: none; }
.history-heading { flex: 1; min-width: 0; }
.history-title { font-size: 20px; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.history-date { font-size: 14px; font-weight: 700; color: var(--sub); margin: 20px 4px 8px; }
.entry {
  background: var(--card);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  margin-bottom: 8px;
}
.entry-meta { display: flex; justify-content: space-between; gap: 8px; color: var(--sub); font-size: 13px; }
.entry-amount { color: var(--text); font-weight: 700; }
/* 改行をそのまま表示する */
.entry-note { margin-top: 6px; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.6; }
.entry-reflection {
  margin-top: 8px;
  padding: 10px 12px;
  background: var(--streak-bg);
  border-radius: 10px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.6;
  font-size: 15px;
}
.entry-reflection::before { content: "💡 "; }
.entry-empty { margin-top: 6px; color: var(--sub); font-size: 14px; }

/* ---- 下のタブ ---- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr)); /* 4つを必ず同じ幅に */
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  background: none;
  color: var(--sub);
  border-radius: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.3px;
  white-space: nowrap;
  padding: 16px 0 18px;
  /* 選んでいるタブの上に線を出すための場所 */
  border-top: 3px solid transparent;
}
.tabbar button.active { color: var(--accent); font-weight: 800; border-top-color: var(--accent); }
/* iPhone SE など幅の狭い画面では、「タイムライン」が入りきるよう少しだけ小さく */
@media (max-width: 360px) {
  .tabbar button { font-size: 13.5px; letter-spacing: -0.5px; }
}

/* ---- 下に一瞬出るお知らせ ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 20px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

/* ---- ふりかえりタブ ---- */
.reflection-hint { margin: -4px 4px 12px; font-size: 13px; }
#reflection-list { display: grid; gap: 12px; }
.reflection-card {
  padding: 14px 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.reflection-card:active { opacity: 0.7; }
.reflection-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--sub); font-size: 13px; }
/* 対象（習慣名 or 全体）の小さな札 */
.reflection-meta .target {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px 10px;
  border: 1.5px solid var(--head-line);
  border-radius: 8px;
  background: var(--head-bg);
  color: var(--text);
  font-weight: 700;
}
.reflection-meta .target.all { border-color: var(--streak); background: var(--streak-bg); color: var(--streak); }
.reflection-title { margin-top: 8px; font-size: 17px; font-weight: 800; overflow-wrap: anywhere; }
/* 改行をそのまま表示する */
.reflection-body { margin-top: 6px; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.7; }
#reflection-list + .message + .wide { margin-top: 12px; }

#reflection-dialog { max-height: 92vh; overflow-y: auto; }
/* 削除は目立たせすぎない（押し間違い防止のため、保存ボタンから離して下に置く） */
button.danger-link {
  display: block;
  margin: 16px auto 0;
  padding: 8px 12px;
  background: none;
  color: var(--error);
  font-size: 14px;
}

/* ---- タイムラインタブ ---- */
.timeline-filters { display: grid; grid-template-columns: 1fr; gap: 8px; margin-bottom: 4px; }
.timeline-filters input, .timeline-filters select { margin-top: 0; background: var(--card); }
@media (min-width: 640px) {
  .timeline-filters { grid-template-columns: 2fr 1fr; }
}
.tl-head { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.tl-habit { color: var(--text); font-weight: 800; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tl-time { flex: none; }
/* ふりかえり：目立つ色（左に太い線） */
.entry.tl-reflection {
  background: var(--streak-bg);
  border-left: 4px solid var(--streak);
  cursor: pointer;
}
.entry.tl-reflection:active { opacity: 0.7; }
.tl-kind { color: var(--streak); font-weight: 700; }
.tl-reflection .reflection-title { margin-top: 4px; }
/* 変更履歴：控えめな1行 */
.tl-event {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 16px 10px;
  color: var(--sub);
  font-size: 13px;
}
.tl-event-text { min-width: 0; overflow-wrap: anywhere; }
#timeline-more { margin-top: 12px; }

/* 押すと直せる記録（履歴・タイムライン） */
.entry.tappable { cursor: pointer; }
.entry.tappable:active { opacity: 0.7; }

/* ---- 連続日数・達成率 ---- */
/* 履歴画面の上に3つ並べて大きく */
.stat-tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 8px 0 4px; }
.stat-tiles[hidden] { display: none; }
.stat-tile { padding: 10px 8px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; text-align: center; }
.stat-label { color: var(--sub); font-size: 12px; font-weight: 700; }
.stat-big { margin-top: 2px; font-size: 22px; font-weight: 800; color: var(--accent); letter-spacing: -0.5px; white-space: nowrap; }
.stat-small { margin-top: 2px; color: var(--sub); font-size: 11.5px; }

/* タイムラインの上の「達成率」（▼で開け閉め） */
.tl-stats {
  margin: 4px 0 4px;
  padding: 4px 14px 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.tl-stats[hidden] { display: none; }
.tl-stats summary {
  padding: 8px 0 4px;
  color: var(--sub);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.tl-stats:not([open]) { padding-bottom: 4px; }
.tl-stats .stat-tiles { margin: 4px 0 0; }
.stats-row {
  display: grid;
  /* 行ごとに幅が変わらないよう、数字の列は幅を決めておく（縦にそろう） */
  grid-template-columns: minmax(0, 1fr) 56px 48px 48px;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
  cursor: pointer;
}
.stats-row:first-child { border-top: none; }
/* 見出しの行：押せない・小さい文字 */
.stats-row.stats-head { padding: 0 0 2px; color: var(--sub); font-size: 11px; font-weight: 700; cursor: default; }
.stats-head span:nth-child(2) { text-align: center; }
.stats-row:active { opacity: 0.6; }
/* 長い習慣名は2行まで折り返す（狭い画面で「腕立て…」のように切れないように） */
.stats-name { font-weight: 700; line-height: 1.3; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.badge.none { color: var(--sub); background: var(--bg); }
.stats-num { text-align: right; white-space: nowrap; }
.stats-row .badge { justify-self: center; }
b.stats-num { font-size: 15px; color: var(--accent); }

/* ---- 次にやること ---- */
/* 「今日」の行：名前の右に札（長いときは「…」で切る） */
.row-line { min-width: 0; }
.row-line .row-name { flex: none; max-width: 60%; }
.next-chip {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--next-bg);
  color: var(--next);
  font-size: 13px;
  font-weight: 700;
}
.next-chip.empty { background: none; border: 1px dashed var(--line); color: var(--sub); font-weight: 600; }
.row-sub .badge { margin-left: 2px; }
.next-detail { color: var(--next); font-weight: 600; }

/* 記録フォームの「次にやること」 */
.next-box { margin-top: 14px; padding: 12px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); }
.next-box[hidden] { display: none; }
.next-label { font-size: 14px; color: var(--sub); }
.next-choices { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.next-choices:empty { display: none; }
button.choice {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--card);
  border: 1.5px solid var(--line);
  color: var(--text);
  font-size: 14px;
}
button.choice.on { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--card)); }
.next-box input[hidden] { display: none; }

/* ボタン付きのお知らせ（○で記録したあとの「続ける」「変える」） */
.toast.has-actions { display: flex; align-items: center; gap: 8px; border-radius: 14px; width: calc(100% - 32px); max-width: 440px; }
.toast.has-actions > span { flex: 1; min-width: 0; }
.toast-button { padding: 6px 10px; border-radius: 10px; background: var(--bg); color: var(--text); font-size: 13px; flex: none; }
.toast.has-actions.show { pointer-events: auto; } /* 見えているときだけ押せる */

/* 達成済みの行：名前の右に小さく「次にやること」 */
/* 達成済みは横幅が足りないので、名前の下の2行目に出す */
.done-name { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 3px; }
.done-name .next-chip { max-width: 100%; }
.done-row .done-name .row-name { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.next-chip.small { font-size: 12px; padding: 1px 7px; }

/* ---- 並べ替え（ドラッグ＆ドロップ） ---- */
/* 長押ししたときに、iPhone が文字を選んだりメニューを出したりしないように */
.cat-card[data-id], .todo-rows li, .done-rows li, .rows li[data-id] {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
/* 置き場所の目印（元の場所に残る影） */
.drag-ghost { opacity: 0.35; }
/* 指についてくる方 */
.drag-moving { opacity: 0.95 !important; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); transform: scale(1.02); }
.drag-hint { margin: 8px 4px 0; font-size: 13px; }

/* ---- ログイン画面（ほかの画面・下のタブ・お知らせより上に全面で出す） ---- */
.login-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--bg);
}
.login-box {
  width: 100%;
  max-width: 360px;
  padding: 24px;
  background: var(--card);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.login-box h1 { margin: 0; font-size: 26px; }
.login-box input { margin: 16px 0 12px; }
