:root {
  --bg: #ffffff; --fg: #111; --muted: #737373; --line: #dbdbdb; --soft: #f5f5f5;
  --link: #00376b; --accent: #0095f6; --red: #ed4956;
  --ring: linear-gradient(45deg, #f9a825, #e91e63 55%, #7e3ff2);
  --card-bg: #fafafa;
}
:root[data-theme="dark"] {
  --bg: #000; --fg: #f5f5f5; --muted: #a8a8a8; --line: #262626; --soft: #121212;
  --link: #e0f1ff; --card-bg: #1e1e1e;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #000; --fg: #f5f5f5; --muted: #a8a8a8; --line: #262626; --soft: #121212;
    --link: #e0f1ff; --card-bg: #1e1e1e;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { color-scheme: light dark; }
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Hiragino Sans", "Noto Sans JP", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- login ---------- */
#login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
#loginForm { width: min(340px, 100%); display: grid; gap: 12px; text-align: center; }
#loginForm input[type=password] {
  padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--soft); color: var(--fg); font-size: 16px;
}
.primary { background: var(--accent); color: #fff; padding: 10px 14px; border-radius: 8px; font-weight: 600; }
.danger { color: var(--red); font-weight: 600; padding: 10px 14px; }
.err { color: var(--red); min-height: 1.2em; }
.sub { color: var(--muted); margin: -4px 0 8px; }
.logo { font-family: "Snell Roundhand", "Segoe Script", "Brush Script MT", cursive; font-size: 28px; font-weight: 700; letter-spacing: .5px; }
.logo.big { font-size: 46px; }

/* ---------- top bar & stories ---------- */
.top {
  position: sticky; top: 0; z-index: 20; background: var(--bg); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 12px; padding: 8px 14px; padding-top: calc(8px + env(safe-area-inset-top));
  max-width: 470px; margin: 0 auto;
}
.top .logo { margin-right: auto; }
.counts { display: flex; gap: 8px; font-variant-numeric: tabular-nums; font-weight: 600; }
.counts b:nth-child(1) { color: #1a73e8; } .counts b:nth-child(2) { color: #d93025; } .counts b:nth-child(3) { color: #188038; }
.icon { width: 28px; height: 28px; }
.icon svg { width: 24px; height: 24px; }
.stories {
  display: flex; gap: 14px; overflow-x: auto; padding: 12px 14px; max-width: 470px; margin: 0 auto;
  scrollbar-width: none; border-bottom: 1px solid var(--line);
}
.stories::-webkit-scrollbar { display: none; }
.story { flex: 0 0 auto; width: 66px; text-align: center; }
.ring { width: 66px; height: 66px; border-radius: 50%; padding: 3px; background: var(--ring); display: grid; place-items: center; position: relative; }
.ring.off { background: var(--line); }
.ring .av { width: 100%; height: 100%; border-radius: 50%; border: 3px solid var(--bg); background: var(--soft); display: grid; place-items: center; font-size: 24px; font-weight: 600; }
.ring.sel { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--fg); }
.story .badge { position: absolute; right: -2px; top: -2px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px; background: var(--red); color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center; border: 2px solid var(--bg); }
.story .nm { display: block; margin-top: 4px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- feed ---------- */
#feed, #end { max-width: 470px; margin: 0 auto; }
.post { border-bottom: 1px solid var(--line); padding-bottom: 6px; scroll-margin-top: 58px; transition: opacity .3s; }
.post.done { opacity: .62; }
.ph { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.ph .ring { width: 38px; height: 38px; padding: 2px; flex: none; }
.ph .ring .av { font-size: 16px; border-width: 2px; }
.ph .who { display: grid; line-height: 1.2; min-width: 0; }
.ph .who b { font-weight: 600; }
.ph .who span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip { margin-left: auto; font-size: 11px; font-weight: 700; letter-spacing: .04em; padding: 3px 8px; border-radius: 999px; background: var(--soft); border: 1px solid var(--line); color: var(--muted); }
.chip.new { color: #1a73e8; } .chip.learning, .chip.relearning { color: #d93025; } .chip.review { color: #188038; }

.media { --mh: max(calc(100dvh - 235px), min(100vw, 470px)); position: relative; background: var(--card-bg); min-height: min(100vw, 470px); display: flex; cursor: pointer; user-select: none; -webkit-user-select: none; overflow: hidden; }
.media .host { display: block; width: 100%; max-height: var(--mh); overflow-y: auto; scrollbar-width: none; }
.media .host::-webkit-scrollbar { display: none; }
.media.flip .host { animation: flip .26s ease; }
@keyframes flip { 0% { transform: scaleX(1); } 50% { transform: scaleX(0.02); } 100% { transform: scaleX(1); } }
.dots { position: absolute; left: 0; right: 0; bottom: 8px; display: flex; justify-content: center; gap: 5px; pointer-events: none; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: #8e8e8e66; }
.dots i.on { background: var(--accent); }
.hint { position: absolute; top: 8px; right: 8px; font-size: 11px; color: var(--muted); background: var(--bg); opacity: .85; padding: 2px 8px; border-radius: 10px; pointer-events: none; }
.post.flipped .hint, .post.done .hint { display: none; }

.acts { display: flex; gap: 4px; padding: 8px 8px 0; }
.act { flex: 1; display: grid; justify-items: center; gap: 1px; padding: 6px 2px; border-radius: 10px; opacity: .55; transition: opacity .15s, transform .1s; }
.act svg { width: 28px; height: 28px; }
.act span { font-size: 11px; font-weight: 600; }
.act em { font-style: normal; font-size: 11px; color: var(--muted); }
.post.flipped .act { opacity: 1; }
.act:active { transform: scale(.92); }
.act.on { opacity: 1; }
.act.on.again svg { fill: var(--red); stroke: var(--red); }
.act.on.hard svg { fill: #f5a623; stroke: #f5a623; }
.act.on.good svg { fill: #1a73e8; stroke: #1a73e8; }
.act.on.easy svg { fill: #188038; stroke: #188038; }
.post.done .act:not(.on) { opacity: .25; pointer-events: none; }
.act:disabled { pointer-events: none; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.35); } 100% { transform: scale(1); } }
.act.on svg { animation: pop .3s ease; }

.cap { padding: 4px 12px 8px; }
.rated { font-weight: 600; min-height: 1.4em; }
.tags { color: var(--link); word-break: break-word; }
.tags span { margin-right: 6px; }
.model { color: var(--muted); font-size: 12px; margin-top: 2px; }

#end { text-align: center; padding: 36px 20px 80px; color: var(--muted); }
#end .check { width: 64px; height: 64px; border-radius: 50%; margin: 0 auto 10px; display: grid; place-items: center; background: var(--ring); color: #fff; }
#end .check svg { width: 34px; height: 34px; stroke-width: 3; }
#end h4 { margin: 0 0 4px; color: var(--fg); font-size: 16px; }
#end button { margin-top: 12px; }
#sentinel { height: 1px; }

#toast {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translate(-50%, 140%);
  background: #262626; color: #fff; padding: 10px 14px; border-radius: 10px; display: flex; gap: 14px; align-items: center;
  z-index: 50; transition: transform .25s; max-width: 92vw; box-shadow: 0 6px 24px #0006;
}
#toast.show { transform: translate(-50%, 0); }
#toast button { color: #6cb7ff; font-weight: 700; }

dialog { border: 0; border-radius: 14px; padding: 18px; width: min(360px, 92vw); background: var(--bg); color: var(--fg); box-shadow: 0 10px 40px #0007; }
dialog::backdrop { background: #0008; }
dialog h3 { margin: 0 0 10px; }
dialog label { display: grid; gap: 4px; margin: 10px 0; }
dialog label.row { display: flex; gap: 8px; align-items: center; }
dialog select { padding: 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--soft); color: var(--fg); }
.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; margin: 12px 0; color: var(--muted); font-size: 13px; }
.legend b { grid-column: 1 / -1; color: var(--fg); }
.decks { margin: 12px 0; }
.dhead { display: flex; gap: 8px; align-items: baseline; }
.dhead span { color: var(--muted); font-size: 13px; flex: 1; }
.dhead button { color: var(--accent); font-weight: 600; font-size: 13px; }
#deckTree { max-height: 40vh; overflow-y: auto; margin-top: 4px; border: 1px solid var(--line); border-radius: 8px; padding: 4px 0; }
.drow { display: flex; align-items: center; }
.drow .tw { width: 26px; height: 30px; flex: 0 0 auto; color: var(--muted); }
dialog .drow label { display: flex; align-items: center; gap: 8px; margin: 0; flex: 1; min-width: 0; padding: 5px 8px 5px 0; }
.drow label span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.drow small { color: var(--muted); font-size: 12px; }
.dlg-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }

@media (min-width: 520px) {
  body { background: var(--bg); }
  .top, .stories, #feed, #end { border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
}

#boot { max-width: 470px; margin: 60px auto; padding: 0 24px; text-align: center; display: grid; gap: 8px; color: var(--muted); }
#boot b { color: var(--fg); font-size: 16px; }
.spin { width: 34px; height: 34px; margin: 0 auto 6px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: sp 1s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }

/* ---------- text posts (example sentences) ---------- */
.tpost .ph .ring .av { font-size: 15px; }
.tmedia { position: relative; min-height: min(80vw, 380px); display: flex; align-items: center; cursor: pointer; user-select: none; -webkit-user-select: none;
  background: linear-gradient(135deg, var(--c1), var(--c2)); color: #fff; text-shadow: 0 1px 3px #0003; }
.tbody { width: 100%; padding: 34px 22px; text-align: center; }
.tmedia .jp { margin: 0; font-size: clamp(22px, 6.4vw, 30px); line-height: 1.9; font-weight: 600; word-break: break-word; }
.tmedia rt { font-size: .45em; font-weight: 500; opacity: .95; }
:root[data-furigana="off"] .tmedia rt, :root[data-furigana="tap"] .tpost:not(.open) .tmedia rt { visibility: hidden; }
.tmedia .tr { display: none; margin-top: 14px; padding-top: 12px; border-top: 1px solid #ffffff66; }
.tpost.open .tmedia .tr { display: block; animation: fadein .25s ease; }
.tmedia .en { margin: 0; font-size: 17px; font-weight: 600; line-height: 1.4; }
.tmedia .vi { margin: 6px 0 0; font-size: 13px; opacity: .85; }
.tmedia .hint { color: #fff; background: #0003; text-shadow: none; }
.tpost.open .hint { display: none; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.tcap { display: none; }
.tpost.open .tcap { display: block; animation: fadein .25s ease; }
.vocab { list-style: none; margin: 4px 0 0; padding: 0; display: grid; gap: 7px; }
.vocab li { line-height: 1.35; }
.vocab li b { font-weight: 600; font-size: 15px; margin-right: 6px; }
.vocab li .rd { color: var(--muted); margin-right: 6px; }
.vocab li i { font-style: normal; color: #f5a623; margin-right: 6px; }
.vocab li .gl { display: block; color: var(--fg); opacity: .85; font-size: 13px; }
.vocab li.head b { color: var(--accent); }
