:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-grouped: #f2f2f7;
  --fill: #f2f2f7;
  --fill-2: #e5e5ea;
  --label: #000000;
  --label-2: rgba(60, 60, 67, 0.62);
  --label-3: rgba(60, 60, 67, 0.32);
  --separator: rgba(60, 60, 67, 0.16);
  --accent: #4f46e5;
  --on-accent: #ffffff;
  --accent-soft: rgba(79, 70, 229, 0.14);
  --accent-strong: rgba(79, 70, 229, 0.32);
  --danger: #ff3b30;
  --bar: rgba(249, 249, 249, 0.86);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  --splash: #4b43d6;
  --ribbon: #ffc74d;

  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: ui-serif, "New York", "Iowan Old Style", "Charter", Georgia, "Times New Roman", serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --gutter: 16px;
  --tabbar-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-grouped: #000000;
    --fill: #1c1c1e;
    --fill-2: #2c2c2e;
    --label: #ffffff;
    --label-2: rgba(235, 235, 245, 0.62);
    --label-3: rgba(235, 235, 245, 0.3);
    --separator: rgba(84, 84, 88, 0.6);
    --accent: #818cf8;
    --on-accent: #000000;
    --accent-soft: rgba(129, 140, 248, 0.18);
    --accent-strong: rgba(129, 140, 248, 0.38);
    --danger: #ff453a;
    --bar: rgba(22, 22, 24, 0.86);
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font: 17px/1.4 var(--sans);
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom));
}
body.reading { overflow: hidden; }

[hidden] { display: none !important; }

a { color: var(--accent); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: 0.4; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.ic {
  width: 1.15em; height: 1.15em; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
  vertical-align: -0.2em;
}
.ic.big { width: 44px; height: 44px; stroke-width: 1.5; color: var(--label-3); }

.serif { font-family: var(--serif); }
.mono { font-family: var(--mono); font-size: 15px; }
.clamp-1, .clamp-2, .clamp-3 { display: -webkit-box; -webkit-box-orient: vertical; overflow: hidden; }
.clamp-1 { -webkit-line-clamp: 1; }
.clamp-2 { -webkit-line-clamp: 2; }
.clamp-3 { -webkit-line-clamp: 3; }

/* ---------- Layout ---------- */

.pane {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 8px) var(--gutter) 32px;
}

.nav {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0 8px;
}
.large-title { margin: 0; font-size: 34px; line-height: 1.15; font-weight: 700; letter-spacing: -0.02em; }
.nav-actions { display: flex; align-items: center; gap: 4px; margin-left: auto; }

.icon-btn {
  display: inline-grid; place-items: center;
  min-width: 44px; height: 44px; padding: 0 8px;
  border: 0; border-radius: 22px; background: none; color: var(--accent);
  font-size: 20px;
}
.icon-btn:active:not(:disabled) { background: var(--accent-soft); }
.icon-btn.subtle { color: var(--label-3); font-size: 17px; }
.icon-btn.subtle:hover { color: var(--danger); }
.icon-btn.txt { display: inline-flex; align-items: baseline; justify-content: center; padding-top: 12px; font: 600 17px var(--serif); }
.icon-btn.txt small { font-size: 12px; margin-left: 1px; }
.icon-btn.spinning .ic { animation: spin 0.9s linear infinite; }

.text-btn {
  min-height: 44px; padding: 0 8px;
  border: 0; background: none; color: var(--accent);
  display: inline-flex; align-items: center; gap: 6px;
}
.text-btn.strong { font-weight: 600; }
.text-btn.danger { color: var(--danger); }
.text-btn.center { display: flex; margin: 4px auto 0; }

.primary, .secondary, .danger {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 50px; padding: 0 20px;
  border: 0; border-radius: 14px;
  font-weight: 600;
}
.primary { background: var(--accent); color: var(--on-accent); }
.primary:active:not(:disabled) { filter: brightness(0.92); }
.secondary { background: var(--accent-soft); color: var(--accent); min-height: 44px; width: auto; display: inline-flex; }
.danger { background: var(--danger); color: #fff; min-height: 44px; }
.danger-link {
  display: block; margin: 4px auto 0; min-height: 44px; padding: 0 12px;
  border: 0; background: none; color: var(--danger); font-size: 15px;
}

.section-title {
  display: block; margin: 24px 0 8px;
  font-size: 13px; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase;
  color: var(--label-2);
}
.footnote { margin: 8px 2px 0; font-size: 13px; color: var(--label-2); }
.field-label { display: block; margin: 20px 0 6px; font-size: 13px; font-weight: 600; color: var(--label-2); }
.group { margin-top: 8px; }
.error { display: flex; gap: 6px; align-items: flex-start; margin: 10px 2px 0; color: var(--danger); font-size: 14px; }

.input {
  display: block; width: 100%;
  padding: 12px 14px;
  border: 0; border-radius: 12px;
  background: var(--fill);
  font-size: 17px;
  resize: vertical;
}
textarea.input { line-height: 1.4; min-height: 48px; }
.input::placeholder { color: var(--label-3); }
.input:focus { outline: 2px solid var(--accent-strong); outline-offset: 0; }
.input-row { display: flex; gap: 6px; align-items: center; }

/* ---------- Chips & segmented ---------- */

.scroller {
  display: flex; gap: 8px;
  overflow-x: auto; scrollbar-width: none;
  margin: 0 calc(-1 * var(--gutter)); padding: 4px var(--gutter);
  scroll-padding: var(--gutter);
}
.scroller::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 36px; padding: 0 14px; margin: 4px 0;
  border: 0; border-radius: 18px;
  background: var(--fill); color: var(--label);
  font-size: 15px; white-space: nowrap; text-decoration: none;
}
.chip[aria-checked="true"] { background: var(--accent); color: var(--on-accent); font-weight: 600; }
.chip .ic { color: var(--accent); }
.chip.small { min-height: 32px; padding: 0 12px; font-size: 14px; color: var(--accent); background: var(--accent-soft); margin: 2px 0; }
.inspirations .chip { font-size: 14px; }

.segmented {
  display: flex; padding: 2px; border-radius: 10px; background: var(--fill-2);
}
.segmented button {
  flex: 1; min-height: 36px; border: 0; border-radius: 8px; background: none;
  font-size: 14px; font-weight: 500;
}
.segmented button[aria-checked="true"] { background: var(--bg); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); font-weight: 600; }
@media (prefers-color-scheme: dark) {
  .segmented button[aria-checked="true"] { background: #636366; }
}

/* ---------- Lists ---------- */

.list { list-style: none; margin: 0; padding: 0; }
.list > li { border-bottom: 0.5px solid var(--separator); }
.list > li:last-child { border-bottom: 0; }
.row-wrap { display: flex; align-items: center; }
.row, .article, .word-row {
  flex: 1; min-width: 0;
  display: flex; align-items: flex-start; gap: 14px;
  padding: 12px 0;
  color: inherit; text-decoration: none;
}
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.row-title { font-size: 17px; font-weight: 600; line-height: 1.3; }
.row-sub { font-size: 15px; color: var(--label-2); }
.row-meta { font-size: 13px; color: var(--label-2); }
.topline { font-size: 12px; font-weight: 600; color: var(--accent); text-transform: none; }
.article:active, .row:active, .word-row:active { opacity: 0.6; }

.thumb {
  flex: none; width: 96px; height: 72px; border-radius: 10px; overflow: hidden;
  background: var(--fill-2);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.placeholder .bar { display: block; height: 14px; border-radius: 4px; background: var(--fill-2); margin: 4px 0; }
.placeholder .bar.short { width: 30%; height: 10px; }
.placeholder { display: flex; gap: 14px; padding: 12px 0; animation: pulse 1.4s ease-in-out infinite; }

.empty {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  gap: 6px; padding: 56px 24px;
}
.empty h2 { margin: 8px 0 0; font-size: 20px; }
.empty p { margin: 0 0 10px; color: var(--label-2); max-width: 32ch; }

.card {
  padding: 16px; border-radius: 14px; background: var(--fill);
}
.card-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; font-weight: 600; color: var(--label-2);
}
.connect-card { margin: 8px 0 8px; }
.connect-card h2 { display: flex; gap: 8px; align-items: center; margin: 0 0 6px; font-size: 17px; }
.connect-card p { margin: 0 0 12px; font-size: 15px; color: var(--label-2); }

/* ---------- Vocabulary ---------- */

.search { position: relative; margin: 0 0 4px; }
.search .ic { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--label-2); }
.search .input { padding: 9px 12px 9px 38px; border-radius: 10px; }

.word-row { width: 100%; border: 0; background: none; text-align: left; align-items: center; }
.word-line { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.word { font-size: 20px; font-weight: 600; flex: none; max-width: 70%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note { font-size: 15px; color: var(--label-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.word-row .sentence { font-size: 15px; line-height: 1.4; opacity: 0.88; }
.hl { color: var(--accent); font-weight: 700; }

.check {
  flex: none; display: grid; place-items: center;
  width: 24px; height: 24px; border-radius: 50%;
  border: 1.5px solid var(--label-3); color: transparent; font-size: 14px;
}
.word-row[aria-checked="true"] .check { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.select-bar {
  position: fixed; left: 0; right: 0; z-index: 20;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  display: flex; justify-content: space-between; align-items: center;
  padding: 2px max(var(--gutter), calc((100% - 720px) / 2 + var(--gutter)));
  background: var(--bar); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--separator);
}
.pane.selecting { padding-bottom: 80px; }

/* ---------- Tab bar ---------- */

#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: center;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bar); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--separator);
}
#tabbar a {
  flex: 1; max-width: 200px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; font-weight: 500; color: var(--label-2); text-decoration: none;
}
#tabbar a[aria-current="page"] { color: var(--accent); }
#tabbar a[aria-current="page"] .ic { fill: var(--accent-soft); }
.tab-icon { position: relative; font-size: 22px; line-height: 1; }
.badge {
  position: absolute; top: -4px; left: 60%;
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff;
  font: 600 12px/18px var(--sans); text-align: center;
}
body.reading #tabbar { display: none; }

/* ---------- Reader ---------- */

#screen {
  position: fixed; inset: 0; z-index: 40;
  overflow-y: auto; overscroll-behavior: contain;
  background: var(--bg);
  --scale: 1;
}
.reader-nav {
  position: sticky; top: 0; z-index: 2;
  align-items: center;
  padding: calc(var(--safe-top) + 2px) max(8px, calc((100% - 720px) / 2)) 2px;
  background: var(--bar); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
}
.back-btn {
  display: inline-flex; align-items: center; gap: 2px;
  min-height: 44px; padding: 0 8px 0 0;
  border: 0; background: none; color: var(--accent); font-size: 17px;
}
.back-btn .ic { font-size: 24px; }

.reader-body {
  max-width: 680px; margin: 0 auto;
  padding: 16px 20px calc(96px + var(--safe-bottom));
  font-family: var(--serif);
  hyphens: auto; -webkit-hyphens: auto;
  overflow-wrap: break-word;
}
.r-topline {
  margin: 8px 0 6px;
  font: 600 14px var(--sans); letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent);
}
.r-title { margin: 0 0 calc(24px * var(--scale)); font-size: calc(30px * var(--scale)); line-height: 1.12; font-weight: 700; hyphens: manual; }
.r-heading { margin: calc(28px * var(--scale)) 0 calc(10px * var(--scale)); font-size: calc(22px * var(--scale)); line-height: 1.2; font-weight: 600; }
.r-p { margin: 0 0 calc(16px * var(--scale)); font-size: calc(19px * var(--scale)); line-height: 1.6; }
.r-source { margin-top: 24px; font: 13px var(--sans); color: var(--label-2); }
.reader-body .w { cursor: pointer; border-radius: 3px; }
@media (hover: hover) {
  .reader-body .w:hover { background: var(--accent-soft); }
}
.reader-body mark { color: inherit; padding: 0; }
.reader-body mark.saved {
  background: var(--accent-soft);
  text-decoration: underline; text-decoration-color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 3px;
}
.reader-body mark.active { background: var(--accent-strong); border-radius: 3px; }
.reader-body ::selection { background: var(--accent-strong); }

.loading { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 96px 0; font: 15px var(--sans); color: var(--label-2); }
.reader-body .empty { font-family: var(--sans); hyphens: manual; }

.tap-hint, .save-selection {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 3;
  bottom: calc(24px + var(--safe-bottom));
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 16px; border-radius: 999px;
  font: 500 15px var(--sans); white-space: nowrap;
  box-shadow: var(--shadow);
  animation: rise 0.3s ease-out;
}
.tap-hint { background: var(--bar); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); pointer-events: none; }
.save-selection { border: 0; min-height: 44px; background: var(--accent); color: var(--on-accent); font-weight: 600; z-index: 4; }

/* ---------- Sheets ---------- */

dialog.sheet {
  position: fixed; inset: auto 0 0 0;
  width: 100%; max-width: 620px; max-height: min(92dvh, 92vh);
  margin: 0 auto; padding: 0;
  border: 0; border-radius: 16px 16px 0 0;
  background: var(--bg); color: var(--label);
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: sheet-up 0.28s cubic-bezier(0.2, 0.9, 0.3, 1);
}
@media (prefers-color-scheme: dark) { dialog.sheet { background: var(--fill); } dialog.sheet .input, dialog.sheet .card { background: var(--fill-2); } }
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.36); animation: fade 0.2s ease-out; }
.sheet-inner {
  display: flex; flex-direction: column;
  max-height: min(92dvh, 92vh);
  outline: none;
}
.grabber { display: block; width: 36px; height: 5px; border-radius: 3px; background: var(--label-3); margin: 6px auto 0; flex: none; }
.sheet-head {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 0 8px; min-height: 48px; flex: none;
}
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; }
.sheet-body { overflow-y: auto; padding: 4px 20px 20px; overscroll-behavior: contain; }
.sheet-body > .field-label:first-child, .sheet-body > .section-title:first-child { margin-top: 4px; }
.sheet-actions {
  flex: none; display: flex; flex-direction: column; gap: 8px;
  padding: 8px 20px calc(12px + var(--safe-bottom));
  border-top: 0.5px solid var(--separator);
}

.word-input {
  width: 100%; padding: 4px 0; border: 0; background: none;
  font-size: 34px; font-weight: 700; line-height: 1.2;
}
.word-input:focus { outline: none; box-shadow: 0 2px 0 var(--accent); }
.input.big { font-size: 22px; font-weight: 600; }
.lookup { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 12px 0 20px; }
.lookup .footnote { margin: 0; }
.lookup-links { display: contents; }
.card .sentence { margin: 8px 0 0; font-size: 17px; line-height: 1.5; }
.card.meta { display: grid; gap: 10px; margin-top: 16px; font-size: 15px; }
.card.meta > div { display: grid; gap: 2px; }
.card.meta a { display: inline-flex; gap: 6px; align-items: center; text-decoration: none; }

.preview {
  margin: 0; padding: 12px; border-radius: 12px; background: var(--fill);
  font: 12px/1.5 var(--mono); color: var(--label-2);
  white-space: pre-wrap; word-break: break-word; max-height: 11em; overflow: hidden;
}
.confirm { margin-top: 16px; }
.confirm h3 { margin: 0 0 4px; font-size: 17px; }
.confirm p { margin: 0 0 12px; font-size: 15px; color: var(--label-2); }
.confirm-actions { display: flex; gap: 8px; }
.confirm-actions > * { flex: 1; width: auto; }
.export-actions .secondary { width: 100%; }

/* ---------- Toast ---------- */

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 100;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100% - 32px);
  padding: 10px 10px 10px 16px; border-radius: 14px;
  background: #1c1c1e; color: #fff; font-size: 15px;
  box-shadow: var(--shadow);
  animation: rise 0.25s ease-out;
}
#toast button { min-height: 36px; padding: 0 10px; border: 0; border-radius: 8px; background: rgba(255, 255, 255, 0.14); color: #a5b4fc; font-weight: 600; }
body.reading #toast { bottom: calc(24px + var(--safe-bottom)); }

/* ---------- Spinner ---------- */

.spinner {
  display: inline-block; width: 22px; height: 22px; border-radius: 50%;
  border: 2.5px solid var(--fill-2); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
.spinner.light { width: 18px; height: 18px; border-color: rgba(255, 255, 255, 0.35); border-top-color: var(--on-accent); }

/* ---------- Splash ---------- */

#splash {
  position: fixed; inset: 0; z-index: 200;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 28px;
  background: radial-gradient(circle at 50% 38%, rgba(255, 255, 255, 0.18), transparent 420px), var(--splash);
  color: #fff;
  transition: opacity 0.35s ease-out;
}
#splash.out { opacity: 0; pointer-events: none; }
.splash-mark {
  position: relative; width: 148px; height: 148px; margin-top: -40px;
  display: grid; place-items: center; overflow: hidden;
  border-radius: 34px; background: rgba(255, 255, 255, 0.12);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25), 0 12px 48px rgba(0, 0, 0, 0.18);
  animation: letter-in 0.55s cubic-bezier(0.3, 1.4, 0.5, 1) both;
}
.splash-letter { font: 700 96px/1 var(--serif); }
.splash-ribbon { position: absolute; top: 0; right: 22px; width: 26px; height: 44px; fill: var(--ribbon); animation: ribbon-in 0.45s cubic-bezier(0.3, 1.5, 0.5, 1) 0.35s both; }
.splash-text { text-align: center; font-family: var(--serif); animation: text-in 0.4s ease-out 0.55s both; }
.splash-title { font-size: 40px; font-weight: 700; }
.splash-sub { font-size: 16px; opacity: 0.8; margin-top: 4px; }

/* ---------- Animations ---------- */

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: 0.5; } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 12px); } }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes letter-in { from { opacity: 0; transform: scale(0.86); } }
@keyframes ribbon-in { from { opacity: 0; transform: translateY(-56px); } }
@keyframes text-in { from { opacity: 0; transform: translateY(12px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-delay: 0ms !important; transition-duration: 0.01ms !important; }
}
