:root {
  --bg: #fbfaf7;
  --fg: #1d1d1f;
  --muted: #74737a;
  --line: #e8e6e1;
  --card: #ffffff;
  --accent: #c8373b;
  --radius: 12px;
  --ja: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #16161a; --fg: #ececef; --muted: #9a99a2; --line: #2b2b31; --card: #1e1e23; --accent: #e5575b; }
  .card .seg.hl { background: color-mix(in srgb, var(--accent) 32%, transparent); }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto;
  max-width: 40rem;
  min-height: 100dvh;
  padding: 0 max(1rem, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(1rem, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[lang="ja"] { font-family: var(--ja); }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }

/* Header and tabs */
.top { display: flex; align-items: center; justify-content: space-between; padding: max(1rem, env(safe-area-inset-top)) 0 0.5rem; }
.brand { font-weight: 600; text-decoration: none; letter-spacing: -0.01em; }
.brand span { color: var(--accent); margin-right: 0.15em; }
.sync { display: inline-flex; align-items: center; gap: 0.4rem; background: none; border: 0; color: var(--muted); font-size: 0.78rem; cursor: pointer; padding: 0.25rem 0; }
.sync i { width: 0.5rem; height: 0.5rem; border-radius: 50%; background: var(--muted); }
.sync[data-state="ok"] i { background: #3a9d5d; }
.sync[data-state="busy"] i { background: #d9a03a; animation: pulse 1s infinite alternate; }
.sync[data-state="offline"] i { background: var(--muted); }
.sync[data-state="error"] i { background: var(--accent); }
@keyframes pulse { to { opacity: 0.3; } }

.tabs { display: flex; gap: 1.25rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; position: sticky; top: 0; background: var(--bg); z-index: 1; }
.tabs a { text-decoration: none; color: var(--muted); padding: 0.6rem 0; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a[aria-current] { color: var(--fg); border-color: var(--accent); }

main { flex: 1; outline: none; }
.foot { display: flex; gap: 1rem; justify-content: center; padding: 1.5rem 0; color: var(--muted); font-size: 0.8rem; }
.foot label { display: inline-flex; gap: 0.35rem; align-items: center; cursor: pointer; }
.foot input { accent-color: var(--accent); }

/* Search */
.search {
  width: 100%; padding: 0.8rem 1rem; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--card); font-size: 1.05rem; outline: none; margin-bottom: 1rem;
}
.search:focus { border-color: var(--muted); }

/* Phrase cards */
.list { display: flex; flex-direction: column; gap: 0.6rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.9rem 1rem 0.6rem; }
.card p { margin: 0; }
.kana { font-size: 0.78rem; color: var(--muted); }
.ja { font-size: 1.35rem; line-height: 1.45; }
.romaji { font-size: 0.88rem; color: var(--muted); font-style: italic; }
.en { margin-top: 0.45rem !important; }
.note { margin-top: 0.4rem !important; font-size: 0.85rem; color: var(--muted); border-left: 2px solid var(--line); padding-left: 0.6rem; }
.actions { display: flex; justify-content: flex-end; align-items: center; gap: 0.25rem; margin-top: 0.2rem; }
.icon { background: none; border: 0; color: var(--muted); width: 2.25rem; height: 2.25rem; border-radius: 50%; cursor: pointer; font-size: 0.95rem; }
.icon:hover { background: var(--line); color: var(--fg); }
.reg { margin-right: auto; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.reg.casual { color: #3a8d9d; }
.reg.formal { color: #9d6a3a; }

/* Segments: tap any piece to highlight it in every form */
.segmented .seg { cursor: pointer; border-radius: 3px; transition: background-color 0.12s; }
.seg.hv { background: color-mix(in srgb, var(--accent) 9%, transparent); }
.seg.hl { background: color-mix(in srgb, var(--accent) 22%, transparent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 22%, transparent); }
.gloss { margin-top: 0.55rem; padding: 0.45rem 0.6rem; border-radius: 8px; background: color-mix(in srgb, var(--accent) 7%, transparent); font-size: 0.88rem; }
.gloss[hidden] { display: none; }
.gloss div { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 0.55rem; }
.g-ja { font-size: 1.05rem; }
.g-kana, .g-romaji { color: var(--muted); }
.g-romaji { font-style: italic; }
.g-mean::before { content: "— "; color: var(--muted); }
.icon.small { width: 1.8rem; height: 1.8rem; font-size: 0.75rem; margin-left: auto; align-self: center; }
.hide-kana .g-kana, .hide-romaji .g-romaji { display: none; }

.hide-kana .kana, .hide-romaji .romaji { display: none; }

/* Practice mode: hidden side reveals on tap */
.quiz.hide-en:not(.revealed) :is(.en, .note),
.quiz.hide-ja:not(.revealed) .ja-block { visibility: hidden; }
.quiz:not(.revealed) { cursor: pointer; position: relative; }
.quiz:not(.revealed)::after { content: "tap to reveal"; position: absolute; right: 1rem; bottom: 0.9rem; font-size: 0.75rem; color: var(--muted); }
.quiz:not(.revealed) .actions { visibility: hidden; }

/* Browse */
.index { list-style: none; margin: 0; padding: 0; }
.index a { display: flex; align-items: baseline; gap: 0.6rem; padding: 0.85rem 0.1rem; border-bottom: 1px solid var(--line); text-decoration: none; }
.index a:hover { color: var(--accent); }
.ja-sub { color: var(--muted); font-size: 0.85rem; }
.count { margin-left: auto; color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; margin-bottom: 0.9rem; flex-wrap: wrap; }
.crumb { color: var(--muted); font-size: 0.9rem; }
.crumb a { color: var(--muted); }
.crumb span:last-child { color: var(--fg); }

.chips { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.label { width: 100%; margin: 0 0 0.2rem; color: var(--muted); font-size: 0.8rem; }
.chip { display: inline-block; padding: 0.3rem 0.75rem; border: 1px solid var(--line); border-radius: 999px; text-decoration: none; font-size: 0.85rem; color: var(--muted); }
.chip:hover { color: var(--fg); border-color: var(--muted); }

/* Random */
.filters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; margin-bottom: 1rem; }
.filters select { width: 100%; min-width: 0; padding: 0.5rem 0.6rem; border: 1px solid var(--line); border-radius: 8px; background: var(--card); font-size: 0.88rem; }
.filters select:disabled { opacity: 0.5; }
.stage { min-height: 12rem; display: flex; flex-direction: column; justify-content: center; }
.stage .card { padding: 1.5rem 1.25rem 0.8rem; }
.stage .ja { font-size: 1.8rem; }
.stage .en { font-size: 1.1rem; }
.pager { display: flex; justify-content: space-between; margin-top: 1rem; }
.pager button { padding: 0.6rem 1.2rem; border-radius: 999px; cursor: pointer; border: 1px solid var(--line); background: none; }
.pager .primary { background: var(--fg); color: var(--bg); border-color: var(--fg); }

.empty { color: var(--muted); text-align: center; padding: 2rem 0; }
.empty p { margin: 0 0 0.75rem; }
.partial { color: var(--muted); font-size: 0.82rem; text-align: center; margin: 1rem 0 0; }
.partial .chip { background: none; cursor: pointer; font: inherit; }
.index .remote a { color: var(--muted); }
.sync[data-state="update"] i { background: #3a72d9; }

@media (max-width: 30rem) {
  .filters { grid-template-columns: 1fr 1fr; }
  .filters select:last-child { grid-column: span 2; }
  .stage .ja { font-size: 1.55rem; }
}
