:root {
  color-scheme: dark;
  font-family: "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  background: #121212;
  color: #f2f2f2;
  --surface: #121212;
  --surface-raised: #202020;
  --text: #f2f2f2;
  --primary: #f2f2f2;
  --primary-text: #121212;
  --primary-active: #d9d9d9;
  --border: #777777;
  --muted: #c8c8c8;
  --focus: #7ab8ff;
  --selected-bg: #f2f2f2;
  --selected-text: #121212;
  --selected-border: #f2f2f2;
}

:root[data-theme="light-standard"] {
  color-scheme: light;
  background: #f5f3ee;
  color: #1a1a1a;
  --surface: #f5f3ee;
  --surface-raised: #ffffff;
  --text: #1a1a1a;
  --primary: #1a1a1a;
  --primary-text: #f5f3ee;
  --primary-active: #333333;
  --border: #77736b;
  --muted: #5b5852;
  --focus: #005fcc;
  --selected-bg: #1a1a1a;
  --selected-text: #f5f3ee;
  --selected-border: #1a1a1a;
}

:root[data-theme="high-contrast"] {
  color-scheme: dark;
  background: #000000;
  color: #ffffff;
  --surface: #000000;
  --surface-raised: #000000;
  --text: #ffffff;
  --primary: #ffffff;
  --primary-text: #000000;
  --primary-active: #dddddd;
  --border: #ffffff;
  --muted: #ffffff;
  --focus: #ffff00;
  --selected-bg: #ffffff;
  --selected-text: #000000;
  --selected-border: #ffffff;
}

@font-face {
  font-family: "Noto Sans JP Embedded";
  src: url("../assets/fonts/NotoSansJP-VF.ttf") format("truetype");
  font-weight: 100 900;
  font-display: swap;
}

@font-face {
  font-family: "Noto Serif JP Embedded";
  src: url("../assets/fonts/NotoSerifJP-VF.ttf") format("truetype");
  font-weight: 100 900;
  font-display: swap;
}

@font-face {
  font-family: "M PLUS 1p Embedded";
  src: url("../assets/fonts/MPLUS1p-Regular.ttf") format("truetype");
  font-weight: 400;
  font-display: swap;
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  background: var(--surface);
  color: var(--text);
}

button, input, select { font: inherit; }
button, label, input, select { -webkit-tap-highlight-color: transparent; }

.app-shell {
  width: min(100%, 48rem);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--surface);
}

.screen {
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}

#setup-screen {
  display: grid;
  grid-template-rows: auto auto auto auto;
  align-content: start;
  gap: .625rem;
}

[hidden] { display: none !important; }

.screen-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: .5rem;
}
.header-actions { display: flex; flex: 0 0 auto; gap: .375rem; }

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.35rem, 6vw, 2rem); }
h2 { font-size: 1.25rem; }
.eyebrow { margin-bottom: .25rem; color: var(--muted); font-size: .8125rem; font-weight: 700; letter-spacing: .08em; }

.panel {
  margin-bottom: 0;
  padding: .75rem;
  border: 1px solid var(--border);
  border-radius: .875rem;
}

.file-panel { text-align: center; }
.file-details { margin: .5rem 0 0; text-align: left; }
.file-details div { display: grid; grid-template-columns: 4.25rem minmax(0, 1fr); gap: .5rem; padding: .15rem 0; }
.file-details dt { color: var(--muted); }
.file-details dd { min-width: 0; margin: 0; }
.file-name-row dd { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta-row dd { display: flex; grid-column: 2; flex-wrap: wrap; gap: .25rem; color: var(--muted); font-size: .8125rem; }

.message { margin: .5rem 0 0; color: var(--muted); line-height: 1.35; }
.settings-panel { display: grid; gap: .625rem; }
.field { display: grid; gap: .25rem; margin: 0; padding: 0; border: 0; }
.field-heading { display: flex; justify-content: space-between; gap: 1rem; }
.field label, .field legend { font-weight: 700; }
input[type="range"] {
  --range-thumb-size: 1.25rem;
  width: 100%;
  min-height: 2.75rem;
  margin: 0;
  appearance: none;
  background: transparent;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: .375rem;
  border-radius: 999px;
  background: var(--border);
}
input[type="range"]::-webkit-slider-thumb {
  width: var(--range-thumb-size);
  height: var(--range-thumb-size);
  margin-top: calc((.375rem - var(--range-thumb-size)) / 2);
  appearance: none;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--primary);
}
input[type="range"]::-moz-range-track {
  height: .375rem;
  border-radius: 999px;
  background: var(--border);
}
input[type="range"]::-moz-range-thumb {
  width: var(--range-thumb-size);
  height: var(--range-thumb-size);
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--primary);
}
.range-ticks {
  --range-thumb-radius: calc(1.25rem / 2);
  position: relative;
  width: 100%;
  min-height: 1.55rem;
  margin-top: -.45rem;
  color: var(--muted);
  font-size: clamp(.625rem, 2.6vw, .75rem);
  font-variant-numeric: tabular-nums;
}
.range-ticks span {
  position: absolute;
  top: 0;
  display: grid;
  min-width: 0;
  transform: translateX(-50%);
  justify-items: center;
  text-align: center;
  line-height: 1.15;
}
.range-ticks span:nth-child(1) { left: var(--range-thumb-radius); }
.range-ticks span:nth-child(2) { left: calc(25% + var(--range-thumb-radius) / 2); }
.range-ticks span:nth-child(3) { left: 50%; }
.range-ticks span:nth-child(4) { left: calc(75% - var(--range-thumb-radius) / 2); }
.range-ticks span:nth-child(5) { left: calc(100% - var(--range-thumb-radius)); }
.range-ticks b { font-size: clamp(.625rem, 2.7vw, .75rem); white-space: nowrap; }
.range-ticks small { font-size: clamp(.5625rem, 2.4vw, .6875rem); font-weight: 400; }
.field-help { margin: 0; color: var(--muted); font-size: .75rem; line-height: 1.25; }
select { width: 100%; min-height: 2.75rem; padding: 0 .75rem; border: 1px solid var(--border); border-radius: .625rem; background: var(--surface-raised); color: var(--text); }

.segmented-control { display: grid; grid-template-columns: repeat(4, 1fr); gap: .375rem; }
.segmented-control input { position: absolute; opacity: 0; pointer-events: none; }
.segmented-control span { display: grid; min-height: 2.75rem; place-items: center; border: 1px solid var(--border); border-radius: .625rem; }
.segmented-control input:checked + span { border-color: var(--selected-border); background: var(--selected-bg); color: var(--selected-text); }
.segmented-control input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.theme-control { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .375rem; }
.theme-control input { position: absolute; opacity: 0; pointer-events: none; }
.theme-control span { display: flex; min-height: 2.75rem; align-items: center; justify-content: center; gap: .15rem; padding: .35rem .25rem; border: 1px solid var(--border); border-radius: .625rem; background: var(--surface-raised); font-size: .8125rem; text-align: center; }
.theme-control small { font-weight: 400; }
.theme-control input:checked + span { border: 3px solid var(--selected-border); padding: calc(.35rem - 2px) calc(.25rem - 2px); background: var(--selected-bg); color: var(--selected-text); }
.theme-control input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }

.button {
  display: inline-grid;
  min-height: 3rem;
  min-width: 3rem;
  place-items: center;
  padding: .7rem 1rem;
  border: 1px solid transparent;
  border-radius: .625rem;
  font-weight: 700;
  cursor: pointer;
}
.button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.button:disabled { cursor: not-allowed; opacity: .45; }
.button-primary { background: var(--primary); color: var(--primary-text); }
.button-primary:active { background: var(--primary-active); }
.button-secondary { border-color: var(--border); background: var(--surface-raised); color: var(--text); }
.button-plain { background: transparent; color: var(--primary); }
.button-wide { width: 100%; }
.button-compact { min-height: 2.75rem; padding: .55rem .7rem; }
.file-picker { width: 100%; }
.start-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
.start-actions.is-single { grid-template-columns: 1fr; }

.reader-screen { display: grid; grid-template-rows: 1fr auto; gap: 1rem; overflow: hidden; }
.reader-text {
  display: flex;
  width: 100%;
  min-width: 0;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  font-size: clamp(2rem, 10vw, 5rem);
  line-height: 1.35;
}
.reader-text.align-left { justify-content: flex-start; text-align: left; }
.reader-text.align-center { justify-content: center; text-align: center; }
.reader-text.align-right { justify-content: flex-end; text-align: right; }
.reader-text.align-justify { justify-content: space-between; text-align: left; }
.reader-text.align-justify.is-single-grapheme { justify-content: center; }
.reader-grapheme { flex: 0 0 auto; white-space: pre; }
.reader-text.is-overflowing { justify-content: flex-start; text-align: left; text-align-last: auto; }
.font-noto-sans { font-family: "Noto Sans JP Embedded", "Noto Sans JP", sans-serif; }
.font-noto-serif { font-family: "Noto Serif JP Embedded", "Noto Serif JP", serif; }
.font-m-plus { font-family: "M PLUS 1p Embedded", "M PLUS 1p", sans-serif; }
.reader-controls { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }

.history-list { display: grid; gap: .75rem; margin: 1rem 0 0; padding: 0; list-style: none; }
.history-item { width: 100%; padding: 1rem; border: 1px solid var(--border); border-radius: .75rem; background: var(--surface-raised); color: var(--text); text-align: left; }
.history-name { display: block; margin-bottom: .375rem; font-weight: 700; overflow-wrap: anywhere; }
.history-date { color: var(--muted); font-size: .875rem; }

dialog { width: min(calc(100% - 2rem), 28rem); padding: 0; border: 1px solid var(--border); border-radius: .875rem; background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgb(13 20 33 / .62); }
.dialog-content { display: grid; gap: .75rem; padding: 1.25rem; }
.dialog-playback-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
.dialog-playback-actions.is-single { grid-template-columns: 1fr; }
.install-message { margin-bottom: 0; color: var(--muted); line-height: 1.6; white-space: pre-line; }

.visually-hidden { position: absolute !important; width: 1px !important; height: 1px !important; padding: 0 !important; margin: -1px !important; overflow: hidden !important; clip: rect(0, 0, 0, 0) !important; white-space: nowrap !important; border: 0 !important; }

@media (min-width: 49rem) {
  .app-shell { box-shadow: 0 0 2rem rgb(23 32 51 / .12); }
  .screen { padding: 2rem; }
}

@media (max-height: 36rem) {
  .screen { padding-top: .75rem; padding-bottom: .75rem; }
  .reader-text { font-size: clamp(1.75rem, 9vw, 4rem); }
}

@media (max-width: 22rem) {
  .theme-control { grid-template-columns: 1fr; }
  .button-compact { padding-inline: .5rem; font-size: .875rem; }
}

@media (max-height: 42rem) {
  .screen { padding-top: max(.5rem, env(safe-area-inset-top)); padding-bottom: max(.5rem, env(safe-area-inset-bottom)); }
  #setup-screen { gap: .375rem; }
  .screen-header { margin-bottom: 0; }
  .eyebrow { display: none; }
  .panel { padding: .5rem; }
  .settings-panel { gap: .375rem; }
  .message { font-size: .8125rem; }
  .file-panel.has-file .message {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
  }
}
.legal-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem 1rem;
  margin-top: 1rem;
  font-size: 0.8rem;
}

.legal-links a {
  color: var(--muted-text);
}
