/* ===========================================================================
   READER LAYOUT
   Loaded last, on purpose. Everything above it stays as it was.

   Three things this fixes.

   The page was 3695px tall on a 900px screen, so almost nothing was visible
   without scrolling. The cause was the matrix canvas sitting in the layout
   and pushing the whole reader below the fold. It is now behind everything
   instead of above it, and the page is exactly one screen tall. Only the text
   and the side panel scroll, and they scroll inside themselves.

   There were three typefaces on one page and labels at two different sizes.
   Now there is one typeface and every label is 14px.

   The surfaces follow GitHub: quiet one pixel borders, small consistent
   corners, generous padding, and nothing shouting. The colours are yours,
   untouched, and the letter rain is still there.
   ======================================================================== */

:root {
  --red: #FC1212;
  --line: rgba(255, 255, 255, .17);
  --line-soft: rgba(255, 255, 255, .09);
  --surface: rgba(0, 0, 0, .72);
  --surface-raised: rgba(10, 10, 10, .88);
  --text: #fff;
  --text-dim: rgba(255, 255, 255, .58);
  --radius: 5px;
  --gap: 14px;
  --label: 14px;
  --ui-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --ui-scale: 1;
  --reading-font-size: 17px;
  --reading-frame-height: 85vh;
  --reading-line-height: 1.8;
  --reading-max-width: 900px;
  --reading-font: "Libre Baskerville", Georgia, "Times New Roman", serif;
  --reading-font-style: normal;
  --panel-font-size: 14px;
}

/* ---------------------------------------------------------------------------
   One screen, no page scroll
   ------------------------------------------------------------------------ */

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
  background: #000;
  color: var(--text);
  font-family: var(--ui-font);
  font-size: var(--label);
}

/* The rain belongs behind the reader, not above it. Fixed so it costs no
   layout height at all, and click-through so it never eats a word click. */
#Matrix {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

/* Everything real sits above the rain. */
#ui-bar,
.wrapper,
#contextMenu {
  position: relative;
  z-index: 2;
}

/* ---------------------------------------------------------------------------
   Top bar
   ------------------------------------------------------------------------ */

#ui-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--surface-raised);
  border-bottom: 1px solid var(--line);
}

#ui-filename {
  color: var(--text-dim);
  font-size: var(--label);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1 1 auto;
}

/* Every control in the bar is the same object with the same weight, which is
   what makes a toolbar read as a toolbar rather than a pile of buttons. */
#ui-bar .ui-btn,
#ui-bar .voice-btn,
.reader-nav a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(0, 0, 0, .55);
  color: var(--text);
  font-family: var(--ui-font);
  font-size: var(--label);
  font-weight: 700;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s, box-shadow .15s, border-color .15s;
}

#ui-bar .ui-btn:hover,
#ui-bar .voice-btn:hover,
.reader-nav a:hover,
#ui-bar .ui-btn:focus-visible,
#ui-bar .voice-btn:focus-visible,
.reader-nav a:focus-visible {
  background: var(--red);
  border-color: var(--red);
  box-shadow: 0 0 10px rgba(252, 18, 18, .55);
  outline: none;
}

.reader-nav {
  display: flex;
  gap: 8px;
  margin-left: 0;
}

#deckCount { font-weight: 400; opacity: .7; }

/* ---------------------------------------------------------------------------
   The two columns
   ------------------------------------------------------------------------ */

.wrapper {
  flex: 1 1 auto;
  min-height: 0;                /* without this the children refuse to shrink */
  display: grid;
  grid-template-columns: minmax(0, 1fr) calc(400px * var(--ui-scale));
  gap: var(--gap);
  padding: var(--gap);
  box-sizing: border-box;
  width: auto;
  max-width: none;
}

.container-left,
.box-right-1 {
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-sizing: border-box;
  overflow: hidden;
}

/* ---------------------------------------------------------------------------
   Reading column
   ------------------------------------------------------------------------ */

.container-left .page {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
  max-width: none;
  width: auto;
}

.chapter-title {
  flex: 0 0 auto;
  padding: 20px 38px 12px;
  color: var(--red);
  font-family: var(--ui-font);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: .04em;
  text-align: left;
}

.divider {
  flex: 0 0 auto;
  height: 1px;
  margin: 0 38px;
  background: var(--line-soft);
  border: 0;
}

/* The only part of the reading column that scrolls, and it scrolls inside
   itself so the controls underneath never move. */
.container-left .content {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 30px 38px 34px;
  max-width: none;
  width: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.container-left .content::-webkit-scrollbar { width: 10px; }
.container-left .content::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 999px;
}
.container-left .content::-webkit-scrollbar-track { background: transparent; }

#controls {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 10px;
  border-top: 1px solid var(--line-soft);
  background: rgba(0, 0, 0, .5);
  position: static;
}

#controls .key {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font-size: var(--label);
  cursor: pointer;
  transition: background .15s;
}

#controls .key:hover { background: var(--red); border-color: var(--red); }

#page-number {
  color: var(--text-dim);
  font-size: var(--label);
  font-variant-numeric: tabular-nums;
  min-width: 74px;
  text-align: center;
}

/* Left over from an older layout. It held the word "testing" and took up
   space in the middle of the reader. */
#word-container { display: none !important; }

/* The floating chevrons duplicated the arrows in the control bar and sat on
   top of the text. */
.arrow.left, .arrow.right { display: none !important; }

/* ---------------------------------------------------------------------------
   Side panel
   ------------------------------------------------------------------------ */

.box-right-1 {
  padding: 0 !important;
}

.tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--line);
  background: rgba(0, 0, 0, .5);
}

/* GitHub's tab: no box, just a line under the one you are on. */
.tabs .tabButton {
  flex: 1 1 auto;
  padding: 11px 10px;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  color: var(--text-dim);
  font-family: var(--ui-font);
  font-size: var(--label);
  font-weight: 700;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}

.tabs .tabButton:hover { color: var(--text); }

.tabs .tabButton.is-active {
  color: var(--text);
  border-bottom-color: var(--red);
}

.tabContent {
  flex: 1 1 auto;
  min-height: 0 !important;
  max-height: none !important;
  overflow-y: auto;
  padding: 16px 18px;
  font-size: var(--label);
  line-height: 1.6;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.tabContent::-webkit-scrollbar { width: 10px; }
.tabContent::-webkit-scrollbar-thumb { background: var(--line); border-radius: 999px; }

/* Only the open one takes part in the layout. */
.tabContent[style*="display: none"] { display: none !important; }

/* Dictionary entries, kept quiet so the definitions read easily. */
.dictionary-entry h2 {
  margin: 0 0 12px;
  color: var(--red);
  font-size: 15px;
  font-weight: 700;
}

.dictionary-section { margin: 0 0 14px; }

.dictionary-section strong {
  display: block;
  margin-bottom: 5px;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
}

.dictionary-section p,
.dictionary-section li { font-size: var(--label); }

.dictionary-examples blockquote {
  margin: 0 0 9px;
  padding: 8px 12px;
  border-left: 2px solid var(--line);
  background: rgba(255, 255, 255, .03);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: var(--label);
}

.dictionary-examples blockquote div {
  margin-top: 4px;
  color: var(--text-dim);
  font-size: 13px;
}

/* Chat tab */
#chatHistory {
  max-height: none;
  margin-bottom: 12px;
  font-size: var(--label);
}

.chatbox { display: flex; gap: 8px; flex-wrap: wrap; }

.chatbox input {
  flex: 1 1 140px;
  min-width: 0;
  padding: 8px 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #000;
  color: var(--text);
  font-family: var(--ui-font);
  font-size: var(--label);
}

.chatbox input:focus {
  outline: none;
  border-color: var(--red);
  box-shadow: 0 0 8px rgba(252, 18, 18, .45);
}

.chatbox button,
.box-right-2 .buttonsound {
  padding: 8px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(0, 0, 0, .55);
  color: var(--text);
  font-family: var(--ui-font);
  font-size: var(--label);
  font-weight: 700;
  cursor: pointer;
  transition: background .15s;
}

.chatbox button:hover,
.box-right-2 .buttonsound:hover {
  background: var(--red);
  border-color: var(--red);
}

/* Transport sits under the panel rather than floating in its own box. */
.box-right-2 {
  grid-column: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 0;
  background: none;
  border: 0;
}

.box-right-2 .buttonsound { min-width: 46px; }

/* ---------------------------------------------------------------------------
   The reading text itself
   ------------------------------------------------------------------------ */

.container-left .content,
.reader-word {
  font-family: var(--reading-font);
  font-style: var(--reading-font-style);
}

.container-left .content {
  font-size: var(--reading-font-size);
  line-height: var(--reading-line-height);
  color: rgba(255, 255, 255, .92);
}

/* Keep long lines readable on wide displays while preserving the scrollable
   reading surface. Width is controlled independently from the left panel. */
.container-left #content {
  box-sizing: border-box;
  width: min(100%, var(--reading-max-width));
  margin-inline: auto;
}

.tabContent,
#chatHistory,
.dictionary-entry,
.dictionary-section,
.dictionary-definition,
.speechBubble {
  font-size: var(--panel-font-size);
}

.reader-word { cursor: pointer; border-radius: 3px; }

/* ---------------------------------------------------------------------------
   Narrow screens. The panel goes under the text rather than beside it, and
   the page still never scrolls as a whole.
   ------------------------------------------------------------------------ */

@media (max-width: 1000px) {
  .wrapper {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1.4fr) minmax(0, 1fr) auto;
  }
  .box-right-2 { grid-column: 1; }
}

@media (max-width: 620px) {
  #ui-bar { flex-wrap: wrap; gap: 8px; }
  #ui-filename { order: 3; flex-basis: 100%; }
  .reader-nav { width: 100%; }
  .reader-nav a { flex: 1 1 0; justify-content: center; }
}

/* ===========================================================================
   Corrections after looking at it rendered
   ======================================================================== */

/* The bar was being centred and narrowed by an older rule, leaving black
   margins either side of it while the reader below ran full width. */
#ui-bar {
  width: 100% !important;
  max-width: none !important;
  margin: 0 !important;
  border-radius: 0 !important;
  box-sizing: border-box;
}

/* The chapter title carried a large inherited indent, so it started a long
   way right of the text underneath it. They should share a left edge. */
.container-left .page .chapter-title,
#title {
  padding-left: 38px !important;
  margin: 0 !important;
  text-indent: 0 !important;
  text-align: left !important;
}

/* The right click menu was visible on load, sitting in the middle of the
   page. It should only exist once you have actually asked for it. */
#contextMenu {
  display: none;
  position: fixed;
  z-index: 40;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #000;
  font-family: var(--ui-font);
  font-size: var(--label);
  font-weight: 700;
}

#contextMenu.is-open { display: block; }

/* The transport icons come from Font Awesome, which is not loaded in here, so
   two of the three rendered as empty boxes. Plain characters instead, which
   need no font to arrive and cannot silently disappear. */
.box-right-2 .buttonsound i { display: none; }

.box-right-2 #backwardBtn::after  { content: "◀◀"; }
.box-right-2 #playPauseBtn::after { content: "▶"; }
.box-right-2 #forwardBtn::after   { content: "▶▶"; }
.box-right-2 #playPauseBtn.is-playing::after { content: "❚❚"; }

.box-right-2 .buttonsound {
  min-width: 54px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  letter-spacing: 1px;
}

/* Nothing may push the page taller than the window, at any width. */
.wrapper { overflow: hidden; }
.container-left, .box-right-1, .box-right-2 { min-width: 0; }

@media (max-width: 1000px) {
  .wrapper {
    grid-template-rows: minmax(0, 1.5fr) minmax(0, 1fr) auto;
    overflow: hidden;
  }
  .container-left, .box-right-1 { min-height: 0; overflow: hidden; }
}

@media (max-width: 620px) {
  /* The bar wraps onto two lines here, so the grid has to give that height
     back rather than letting the page grow. */
  #ui-bar { padding: 8px 12px; row-gap: 6px; }
  .wrapper { padding: 10px; gap: 10px; }
  .container-left .content { padding: 22px 22px 26px; font-size: 16px; }
}

/* The wrapper was sizing itself from its contents rather than from the space
   left over, so on a narrow screen the two panels stacked and pushed the page
   past the window. flex-basis auto means "start from my content"; zero means
   "start from nothing and take exactly what is left", which is what a pane
   filling the rest of the window actually wants.

   The stray empty div at the end of the document is hidden for the same
   reason, it was taking a grid row of its own. */
body > .wrapper { flex: 1 1 0 !important; height: 0; min-height: 0; }
body > div:empty { display: none; }

/* On a narrow screen the older stylesheet gave the reading pane a height of
   72vh and the panel 62vh. That is 134vh of content asked to live in one
   screen, which is why they ended up drawn on top of one another. The grid
   rows above already share out whatever height there is, so the two panes
   just need to stop insisting on a size of their own. */
@media (max-width: 1000px) {
  .wrapper .container-left,
  .wrapper .box-right-1 {
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
  }
}

/* ---------------------------------------------------------------------------
   Tabs: the buttons should be the strip

   The strip was 46px tall and the buttons 37px, because the older stylesheet
   put 8px of padding under them and gave each one rounded corners and its own
   outline. So the buttons floated inside a taller box with dead space around
   them instead of filling it.

   Now the strip has no padding of its own and each button stretches the full
   height, so the three of them are the strip. The open one is marked by a line
   underneath rather than a box around it, which is what stops three outlines
   competing with the panel border directly below.
   ------------------------------------------------------------------------ */

.tabs {
  align-items: stretch !important;
  gap: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  min-height: 0 !important;
}

.tabs .tabButton {
  align-self: stretch !important;
  height: auto !important;
  min-height: 0 !important;
  margin: 0 !important;
  padding: 12px 10px !important;
  border: 0 !important;
  border-bottom: 2px solid transparent !important;
  border-radius: 0 !important;
  background: none !important;
  box-shadow: none !important;
}

.tabs .tabButton:hover {
  background: rgba(255, 255, 255, .05) !important;
  border-bottom-color: rgba(255, 255, 255, .25) !important;
  color: var(--text) !important;
}

.tabs .tabButton.is-active {
  background: rgba(255, 255, 255, .05) !important;
  border-bottom-color: var(--red) !important;
  color: var(--text) !important;
}

/* A hairline between them so they read as three separate targets without
   needing an outline each. */
.tabs .tabButton + .tabButton {
  border-left: 1px solid var(--line-soft) !important;
}

/* The chat tab was still outlined in red in four places. The older stylesheet
   sets these with !important, so they have to be answered in kind.

   Send keeps its red fill because it is the one action in that panel that
   does something, but it loses the border so it reads as a filled button
   rather than an outlined one. */
#chatHistory {
  border-color: var(--line) !important;
  border-radius: var(--radius) !important;
}

#userInput,
.chatbox input {
  border-color: var(--line) !important;
  border-radius: var(--radius) !important;
}

#userInput:focus,
.chatbox input:focus {
  border-color: rgba(255, 255, 255, .38) !important;
  box-shadow: none !important;
}

#chat_history,
.chatbox button {
  border-color: var(--line) !important;
  border-radius: var(--radius) !important;
}

#sendBtn {
  border-color: transparent !important;
  border-radius: var(--radius) !important;
}

/* ===========================================================================
   Layout faults the sweep found
   ======================================================================== */

/* THE BAR MUST GROW WHEN IT WRAPS.

   tts-ui.css pins #ui-bar to a fixed 64px (56px on small screens). When the
   controls wrapped to a second or third row, those rows rendered OUTSIDE the
   64px box, and .wrapper below it painted on top and swallowed every click, so
   Library, Review and Fill in the blank became unclickable on any narrow
   screen and after opening a long-titled book on desktop. Letting the bar be
   as tall as its contents fixes both at once. */
#ui-bar {
  height: auto !important;
  min-height: 48px;
  overflow: visible;
  flex-wrap: wrap;
  z-index: 5 !important;              /* above .wrapper, so nothing it holds is covered */
  row-gap: 8px;
}

/* The long filename beside Open a book is what pushes the toolbar wide enough
   to wrap in the first place. Keep it from taking more than its share. */
#ui-filename {
  max-width: 40ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Phone layout must be last: earlier compatibility rules contain fixed
   heights for these same panels. */
@media (max-width: 900px) {
  html,
  body {
    height: auto !important;
    min-height: 100% !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  body > .wrapper {
    display: flex !important;
    flex-direction: column;
    flex: none !important;
    height: auto !important;
    min-height: 0 !important;
    overflow: visible !important;
  }

  .wrapper .container-left {
    flex: none !important;
    height: var(--reading-frame-height) !important;
    min-height: 320px !important;
    max-height: none !important;
  }

  .wrapper .box-right-1 {
    flex: none !important;
    height: 58vh !important;
    min-height: 360px !important;
    max-height: none !important;
  }

  .wrapper .box-right-2 {
    flex: none !important;
    min-height: 76px;
    padding: 12px 0 22px;
  }

  .container-left .page {
    justify-content: flex-start !important;
  }

  .container-left .content {
    flex: 1 1 0 !important;
    display: block !important;
    padding-top: 12px !important;
  }
}

/* ---------------------------------------------------------------------------
   Minimalist / distraction-free reader
   ------------------------------------------------------------------------ */
#minimalistExit { display: none; }

body.minimalist-reader {
  display: block !important;
  height: 100dvh !important;
  min-height: 100dvh !important;
  overflow: hidden !important;
  background: var(--theme-background, #202020) !important;
}

body.minimalist-reader #Matrix,
body.minimalist-reader #ui-bar,
body.minimalist-reader .box-right-1,
body.minimalist-reader .box-right-2,
body.minimalist-reader .divider {
  display: none !important;
}

body.minimalist-reader > .wrapper {
  display: block !important;
  width: 100% !important;
  height: 100dvh !important;
  min-height: 100dvh !important;
  margin: 0 !important;
  padding: 0 !important;
  overflow: hidden !important;
}

body.minimalist-reader .container-left {
  display: flex !important;
  width: 100% !important;
  height: 100dvh !important;
  min-height: 100dvh !important;
  max-height: 100dvh !important;
  margin: 0 !important;
  border: 0 !important;
  border-radius: 0 !important;
  background: var(--theme-background, #202020) !important;
  box-shadow: none !important;
}

body.minimalist-reader .container-left .page {
  width: min(100%, var(--reading-max-width)) !important;
  height: 100% !important;
  margin: 0 auto !important;
  padding: clamp(42px, 8vh, 100px) clamp(24px, 6vw, 76px) 0 !important;
  background: transparent !important;
  box-shadow: none !important;
}

body.minimalist-reader .chapter-title,
body.minimalist-reader #title {
  padding: 0 0 clamp(26px, 6vh, 70px) !important;
  color: var(--theme-text) !important;
  font-family: var(--reading-font) !important;
  font-size: .9em !important;
  font-weight: 400 !important;
  opacity: .72;
}

body.minimalist-reader .container-left #content {
  width: 100% !important;
  max-width: none !important;
  margin: 0 !important;
  padding: 0 0 13vh !important;
  scrollbar-width: none;
}
body.minimalist-reader .container-left #content::-webkit-scrollbar { display: none; }

body.minimalist-reader #controls {
  position: fixed !important;
  right: clamp(18px, 5vw, 60px) !important;
  bottom: max(18px, env(safe-area-inset-bottom)) !important;
  left: auto !important;
  z-index: 80;
  display: block !important;
  width: auto !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  background: transparent !important;
}

body.minimalist-reader #controls .key { display: none !important; }
body.minimalist-reader #page-number {
  min-width: 0;
  color: var(--theme-text) !important;
  font: 400 13px/1.2 var(--ui-font);
  opacity: .65;
}

body.minimalist-reader #minimalistExit {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: 14px;
  z-index: 100;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0 !important;
  border-radius: 50%;
  background: transparent !important;
  color: var(--theme-text) !important;
  font: 300 24px/1 var(--ui-font);
  opacity: .28;
  cursor: pointer;
}
body.minimalist-reader #minimalistExit:hover,
body.minimalist-reader #minimalistExit:focus-visible { opacity: .85; }
