:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #2b2b2b;
  --muted: #8a8a8a;
  --line: #e2ded5;
  --accent: #3f6b5c;
  --accent-text: #ffffff;
  --accent-soft: #e3ece8;
  --danger: #c62828;
  --warn-bg: #fff4e0;
  --warn-line: #f0c36d;
  --edit-bg: #fff8d6;
  --edit-text: #6d5a00;
  --radius: 12px;
  --tabbar-h: 60px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161816;
    --surface: #222522;
    --text: #e8e8e6;
    --muted: #9a9d99;
    --line: #363a36;
    --accent: #6fae97;
    --accent-text: #0e1a15;
    --accent-soft: #24352e;
    --danger: #ff6b6b;
    --warn-bg: #3a2f17;
    --warn-line: #8a6a24;
    --edit-bg: #3a3518;
    --edit-text: #f1df84;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}

button, input, select, textarea { font: inherit; color: inherit; }

[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.topbar-title { font-size: 18px; margin: 0; font-weight: 700; flex: none; }
.backup-status {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: right;
  min-width: 0;
}
.backup-status.warn { color: #b26a00; }
@media (prefers-color-scheme: dark) { .backup-status.warn { color: #f0b354; } }
.btn-reconnect {
  flex: none;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 13px;
  font-weight: 600;
}

main { max-width: 640px; margin: 0 auto; padding: 12px 16px; }

/* ---------- banner ---------- */
.banner {
  margin: 12px 16px 0;
  padding: 12px;
  border-radius: var(--radius);
  font-size: 14px;
}
.banner p { margin: 0 0 8px; }
.banner-warn { background: var(--warn-bg); border: 1px solid var(--warn-line); }
.banner-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* ---------- write view ---------- */
.date-row { display: flex; align-items: center; gap: 8px; }
.date-input {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 12px;
  font-size: 18px;
  font-weight: 600;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
  -webkit-appearance: none;
  appearance: none;
}
.btn-icon {
  flex: none;
  width: 48px;
  height: 48px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 16px;
}
.btn-icon:disabled { opacity: .35; }

.date-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 4px 4px 8px;
  font-size: 14px;
  color: var(--muted);
}
.link-btn {
  border: none;
  background: none;
  color: var(--accent);
  padding: 6px 4px;
  font-size: 14px;
}
.link-btn:disabled { color: var(--muted); opacity: .5; }

.mode-badge {
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 8px;
  margin-bottom: 8px;
  background: var(--accent-soft);
  color: var(--accent);
}
.mode-badge.editing { background: var(--edit-bg); color: var(--edit-text); font-weight: 600; }

.body-input {
  display: block;
  width: 100%;
  min-height: 180px;
  padding: 12px;
  font-size: 17px;
  line-height: 1.7;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  resize: vertical;
}
.body-input:focus, .date-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.count-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin: 6px 4px 10px;
}
.counter { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 15px; flex: none; }
.counter.over { color: var(--danger); font-weight: 700; }
.hint { font-size: 13px; color: var(--danger); }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 16px;
  min-height: 44px;
  font-weight: 600;
}
.btn.block { display: block; width: 100%; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:disabled { opacity: .4; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger-outline { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .4; }
#saveBtn { min-height: 54px; font-size: 18px; }

/* ---------- list view ---------- */
.list-summary { color: var(--muted); font-size: 14px; margin: 0 4px 8px; }
.month-head {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  margin: 16px 4px 6px;
}
.entry {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 8px;
  overflow: hidden;
}
.entry-main {
  flex: 1;
  min-width: 0;
  text-align: left;
  border: none;
  background: none;
  padding: 10px 12px;
}
.entry-date { font-size: 13px; font-weight: 700; color: var(--accent); }
.entry-text { white-space: pre-wrap; word-break: break-word; font-size: 15px; }
.entry-del {
  flex: none;
  width: 48px;
  border: none;
  border-left: 1px solid var(--line);
  background: none;
  color: var(--muted);
  font-size: 18px;
}
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- settings ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card h2 { font-size: 16px; margin: 0 0 8px; }
.stack { display: grid; gap: 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 8px 0 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.field { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.field select {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  padding: 0 8px;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  flex: 1;
  height: var(--tabbar-h);
  border: none;
  background: none;
  color: var(--muted);
  font-size: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.tab-ico { font-size: 20px; line-height: 1; }
.tab.active { color: var(--accent); font-weight: 700; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%) translateY(20px);
  background: rgba(30, 30, 30, .92);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 30;
  max-width: calc(100% - 32px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- dialog ---------- */
.dialog {
  border: none;
  border-radius: 16px;
  padding: 0;
  width: min(92vw, 420px);
  background: var(--surface);
  color: var(--text);
}
.dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog form { padding: 20px; }
.dialog h3 { margin: 0 0 8px; font-size: 17px; }
#dialogBody { font-size: 15px; white-space: pre-wrap; }
#dialogBody select { width: 100%; min-height: 44px; margin-top: 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); padding: 0 8px; }
.dialog-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
