:root {
  color-scheme: light dark;
  --bg: #faf9f7;
  --fg: #1b1a18;
  --muted: #6b665f;   /* 5.41:1 on --bg; #7a756e failed AA */
  --line: #e2ddd6;
  --accent: #2f6f5e;
  --jp-font: "Hiragino Mincho ProN", "Yu Mincho", serif;

  /* Read the device insets once and use the variables everywhere, so every rule
     agrees. env() needs viewport-fit=cover on the viewport meta to be non-zero. */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);

  --corner: 0.5rem;
  --tap: 2.75rem;          /* 44px: the iOS minimum, and above Android's 48dp at 1x */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --fg: #ece9e4;
    --muted: #8d8880;
    --line: #2e3033;
    --accent: #7fc0ab;
  }
}

* { box-sizing: border-box; }

/* Mobile shell, after the pattern Ionic uses. The document itself never scrolls; a
   fixed root owns the viewport and anything that overflows scrolls inside #card.
   That is what actually fixes the phantom scroll: 100vh resolves against the
   toolbar-hidden viewport, so a page sized to it is taller than the visible area the
   moment the toolbar slides in. A page that cannot scroll never collapses the toolbar
   in the first place, so the two viewports stay equal and the mismatch cannot arise. */
html {
  height: 100%;
  overflow: hidden;
  /* y only: the `none` shorthand also sets the x axis, which kills the macOS
     trackpad two-finger swipe-back gesture on desktop. */
  overscroll-behavior-y: none;    /* no pull-to-refresh, no rubber-band */
  touch-action: manipulation;     /* keeps pinch-zoom; drops double-tap zoom + its delay */
  -webkit-text-size-adjust: 100%; /* iOS must not inflate text in landscape */
  text-size-adjust: 100%;
}

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  cursor: pointer;
  background: var(--bg);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  font: 16px/1.7 system-ui, sans-serif;
}

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 1rem;
  overflow: hidden;
  /* Clear the notch, the home indicator, and the top corner buttons. */
  padding:
    calc(var(--safe-top) + 3.5rem)
    calc(var(--safe-right) + 1.25rem)
    calc(var(--safe-bottom) + 1rem)
    calc(var(--safe-left) + 1.25rem);
  -webkit-user-select: none;
  user-select: none;
}

/* The sentence and the dialog copy stay selectable — looking a word up is the one
   text interaction this app wants to allow, and app.js guards taps against it. */
#japanese, #english, #about { -webkit-user-select: text; user-select: text; }

#card {
  display: flex;
  flex-direction: column;
  justify-content: center;
  /* `safe` keeps the top of an over-tall sentence reachable; centred flex items
     otherwise overflow past the scroll container's start edge and get clipped. */
  justify-content: safe center;
  align-items: center;
  gap: 1.75rem;
  max-width: 34rem;
  margin: 0 auto;
  width: 100%;
  min-height: 0;              /* a grid item will not shrink below content without this */
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  text-align: center;
}

#card::-webkit-scrollbar { display: none; }

#japanese {
  margin: 0;
  /* The old 5vw term never won between 320px and 430px, so every phone got the same
     24px. This one reaches: 21.3px at 320 up to 23.9px at 430, same 33.6px desktop cap. */
  font-size: clamp(1.3rem, 0.85rem + 2.4vw, 2.1rem);
  line-height: 1.9;
  font-family: var(--jp-font);
  text-wrap: balance;              /* kills 165/1000 one-character orphan lines */
  min-height: calc(2 * 1.9em);     /* hold two lines so the sentence stops drifting */
}

#english {
  margin: 0;
  font-size: 1.05rem;
  color: var(--muted);
  max-width: 26rem;
  text-wrap: pretty;
  min-height: calc(2 * 1.7em);     /* same reserve on the reveal slot */
}

/* Reserve the reveal slot so the Japanese line does not jump on reveal. */
#english[hidden] {
  display: block !important;
  visibility: hidden;
}

/* The change is animated on the text itself — a short lift and fade in, so the eye
   is told "new sentence" without the page flashing. */
@keyframes enter {
  from { opacity: 0; transform: translateY(0.4rem); }
  to   { opacity: 1; transform: none; }
}

.enter { animation: enter 260ms cubic-bezier(0.2, 0, 0.2, 1) both; }

@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; }
  #refresh.spinning svg { animation: none; }
  * { transition: none !important; }
}

/* Landscape phones get wider and shorter at the same time, so the width-driven clamp
   has to stop growing the sentence here or the vertical budget blows. Measured over
   all 1000 sentences at 844x390 / 736x414 / 844x320: overflow 559/1000 -> 0/1000. */
@media (max-height: 520px) {
  #app {
    padding:
      calc(var(--safe-top) + 3.25rem)
      calc(var(--safe-right) + 1.25rem)
      calc(var(--safe-bottom) + 0.75rem)
      calc(var(--safe-left) + 1.25rem);
    gap: 0.6rem;
  }

  #card { gap: 0.9rem; }
  #japanese { font-size: 1.5rem; line-height: 1.75; }
}

#controls {
  display: flex;
  justify-content: center;
}

button {
  font: inherit;
  padding: 0.6rem 1.2rem;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease;
}

/* Touch devices latch :hover onto the last-tapped element. Left unguarded it sticks
   the accent colour on the furigana toggle and makes it lie about its pressed state. */
@media (hover: hover) {
  button:hover { color: var(--accent); }
}

/* The action button is icon-only; which icon shows is driven by data-state so the
   swap needs no DOM surgery. The `hidden` attribute would not work here — it is an
   HTML attribute and carries no styling for SVG elements. */
#action {
  padding: 0.5rem 1.5rem;
  min-width: var(--tap);
  min-height: var(--tap);
  display: flex;
  align-items: center;
  justify-content: center;
}
#action svg { display: none; }
#action[data-state="hidden"] .icon-eye,
#action[data-state="revealed"] .icon-next { display: block; }

#action svg path,
#action svg circle {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* Focus is the one case that still needs a visible edge. */
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* The dialog is a frame; .about-body is what scrolls. If the dialog scrolled itself,
   the absolutely-positioned close button would scroll away with the content. */
#about {
  position: fixed;
  margin: auto;
  border: 0;
  border-radius: 0.9rem;
  padding: 0;
  max-width: 26rem;
  width: calc(100% - 2.5rem - var(--safe-left) - var(--safe-right));
  max-height: calc(100vh - 2.5rem - var(--safe-top) - var(--safe-bottom));
  max-height: calc(100dvh - 2.5rem - var(--safe-top) - var(--safe-bottom));
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  cursor: default;
  line-height: 1.6;
}

#about[open] { display: flex; flex-direction: column; }

/* padding lives here, not on the dialog: a bare padding ring on #about would be part
   of the dialog element itself, so a thumb landing on it would read as a backdrop
   click and dismiss the dialog by accident. */
.about-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.75rem;
}

#about::backdrop { background: rgb(0 0 0 / 0.5); }

#about h1 {
  margin: 0 0 1rem;
  padding-right: 2rem;
  font-size: 1.15rem;
  font-weight: 500;
  font-family: var(--jp-font);
}

#about h1 span {
  color: var(--muted);
  font: 0.8rem/1 system-ui, sans-serif;
  letter-spacing: 0.08em;
}

#about p { margin: 0 0 0.9rem; font-size: 0.9rem; color: var(--muted); }
#about p:last-child { margin-bottom: 0; }
/* Inline links sit on adjacent lines; the leading keeps their hit boxes from
   overlapping once they are padded out for touch. */
#about p { line-height: 1.75; }
#about a {
  color: var(--accent);
  padding: 0.15rem 0;
  text-underline-offset: 0.2em;
}
#about .version {
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#refresh {
  width: var(--tap);
  height: var(--tap);
  margin: -0.75rem 0;        /* keep the 44px hit box without stretching the line */
  padding: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
}

#refresh svg path {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#refresh.spinning svg { animation: spin 900ms linear infinite; }

@keyframes spin { to { transform: rotate(1turn); } }

#refresh-msg { color: var(--muted); font-size: 0.75rem; }

#about-close {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  z-index: 1;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  display: grid;
  place-items: center;
}

/* Typeface toggle, tucked into the corner so it stays out of the reading path. */
#font, #furigana, #info {
  position: absolute;         /* #app is fixed, so this anchors to the shell */
  z-index: 2;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 1.15rem;
  line-height: 1;
  font-family: var(--jp-font);
}

/* The hit box grew from 36px to 44px; the glyph stays optically where it was because
   the inset shrank by the same 4px each side. */
#font     { top: calc(var(--safe-top) + var(--corner));       right: calc(var(--safe-right) + var(--corner)); }
#furigana { top: calc(var(--safe-top) + var(--corner));       left: calc(var(--safe-left) + var(--corner)); }
#info     { bottom: calc(var(--safe-bottom) + var(--corner)); left: calc(var(--safe-left) + var(--corner)); }

#info svg path, #info svg circle, #about-close svg path {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}

/* Pressed state carries the meaning, since neither button has an outline. */
[aria-pressed="true"] { color: var(--accent); }

/* Readings are always in the DOM and always occupy their line box; the toggle only
   changes whether they are painted. Rendering them on demand would reflow the
   sentence under the reader's eyes. */
#japanese rt {
  font-size: 0.46em;               /* 10.08px -> 11.04px; wrap distribution unchanged */
  font-weight: 400;
  color: var(--muted);
  user-select: none;
}

body:not(.furigana) #japanese rt { visibility: hidden; }
