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

body {
  font-family: var(--font-main);
  font-size: 16px;
  line-height: 1.6;
  color: var(--gray-900);
  background: var(--white);
  min-height: 100%;
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: rgba(212, 130, 15, 0.28);
}

:focus-visible {
  outline: 1px solid var(--gray-300);
  outline-offset: 2px;
}

.shell {
  display: flex;
  min-height: 100dvh;
  background: var(--white);
}

.rail {
  width: 4.75rem;
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 1rem 0.6rem 1rem;
  background: var(--white);
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  margin-bottom: 0.8rem;
  text-decoration: none;
  color: var(--gray-700);
  background: transparent;
  border-radius: 6px;
  font-weight: 700;
  letter-spacing: 0.04em;
  font-size: 0.78rem;
}

.brand:hover {
  background: var(--gray-100);
  color: var(--gray-800);
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  width: 100%;
  flex: 1;
}

.nav-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.55rem 0.2rem;
  color: var(--gray-500);
  text-decoration: none;
  border-radius: 6px;
  font-size: 0.62rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.nav-link i {
  font-size: 20px;
  line-height: 1;
}

.nav-link:hover {
  color: var(--gray-700);
  background: var(--gray-100);
}

.nav-link.is-on {
  color: var(--gray-800);
  background: var(--gray-100);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--gray-500);
  border-radius: 6px;
  cursor: pointer;
  font-size: 20px;
  transition: background-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.icon-btn:hover {
  background: var(--gray-100);
  color: var(--gray-700);
}

.icon-btn:active {
  background: var(--gray-100);
  transform: scale(0.95);
}

.icon-btn.accent {
  background: var(--gray-100);
  color: var(--gray-800);
}

.stage {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 72px;
  padding: 0 var(--space-xl);
  background: var(--white);
  flex: none;
}

.page-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--gray-800);
}

.topbar-meta time {
  font-size: 0.8rem;
  color: var(--gray-500);
  font-variant-numeric: tabular-nums;
}

.views {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.view {
  display: none;
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 1.25rem 1.5rem 2rem;
}

.view.is-active {
  display: flex;
  flex-direction: column;
}

#view-notes.view,
#view-speak.view,
#view-library.view {
  padding: 0;
  overflow: hidden;
}

.keepnote-root,
.tts-app {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
}

.keepnote-root #content-area {
  min-height: 0;
  overflow: auto;
}

.keepnote-root .empty-icon {
  font-size: 2.6rem;
  color: var(--gray-400);
}

/* TTS sits in the desk pane (not 100vh). */
.tts-app {
  height: auto !important;
  max-height: none;
  min-height: 0;
}

@media (max-width: 480px) {
  /* Fallback when .tts-app:not(:has(.mobile-controls-row)). */
  .tts-app:not(:has(.mobile-controls-row)) .header-controls {
    gap: 0.35rem;
  }
  .tts-app:not(:has(.mobile-controls-row)) .header-controls .control-zone-center {
    display: flex !important;
    position: static;
    flex: 1;
    min-width: 0;
    justify-content: flex-end;
    gap: 0.35rem;
  }
  .tts-app:not(:has(.mobile-controls-row)) .control-select-minimal {
    display: block !important;
    max-width: 7.5rem;
    min-width: 0;
    flex: 1;
  }
  .tts-app:not(:has(.mobile-controls-row)) .play-button-primary {
    flex: none;
    width: 2.6rem;
    height: 2.6rem;
  }
}

.keepnote-root #theme-toggle-btn,
.tts-app #theme-toggle-btn,
.tts-app #ccBtn {
  display: none !important;
}

.tts-app .button-flag {
  display: none;
}

.home-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1.4fr);
  gap: 1rem;
  max-width: 1100px;
}

.panel {
  background: var(--white);
  border: none;
  border-radius: 6px;
  padding: var(--space-lg);
}

.panel h2 {
  margin: 0;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--gray-800);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.7rem;
}

.eyebrow {
  margin: 0;
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gray-500);
  font-weight: 600;
}

.clock-time {
  margin: 0.15rem 0;
  font-size: clamp(2.8rem, 7vw, 4.4rem);
  font-weight: 600;
  letter-spacing: -0.04em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--gray-900);
}

.clock-date {
  margin: 0;
  color: var(--gray-500);
}

.quote-panel blockquote {
  margin: 0.35rem 0 0.6rem;
  font-size: 1.05rem;
  font-weight: 400;
  line-height: 1.55;
  color: var(--gray-800);
}

.quote-meta {
  margin: 0;
  font-size: 0.78rem;
  color: var(--gray-500);
}

.quote-actions {
  display: flex;
  gap: 0.35rem;
}

.text-link,
.ghost-btn {
  background: none;
  border: 0;
  color: var(--gray-700);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border-radius: 6px;
}

.text-link:hover,
.ghost-btn:hover {
  color: var(--gray-900);
}

.tool-tiles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
  margin-top: 0.7rem;
}

.tile {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.8rem 0.85rem;
  border: none;
  border-radius: 6px;
  text-decoration: none;
  color: inherit;
  background: transparent;
}

.tile i {
  color: var(--gray-500);
  font-size: 1.2rem;
}

.tile span {
  font-weight: 600;
  font-size: 0.92rem;
}

.tile small {
  color: var(--gray-500);
  font-size: 0.75rem;
}

.tile:hover {
  background: var(--gray-50);
}

.empty {
  color: var(--gray-500);
  font-size: 0.88rem;
  padding: 0.4rem 0;
}

.calc-layout {
  display: grid;
  grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
  gap: 1rem;
  max-width: 960px;
}

.calc-display {
  background: var(--white);
  border: none;
  border-radius: 6px 6px 0 0;
  padding: 0.9rem 1rem 0.5rem;
}

.calc-expr {
  min-height: 1.2rem;
  font-size: 0.82rem;
  color: var(--gray-500);
  font-variant-numeric: tabular-nums;
}

.calc-expr.err { color: var(--accent-red); }

.calc-out {
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--gray-900);
  font: 600 1.7rem/1.2 var(--font-main);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.calc-out:focus,
.calc-out:focus-visible {
  outline: none;
}

.calc-keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
  padding: 0.7rem;
  background: transparent;
  border: none;
  border-radius: 0 0 6px 6px;
}

.key {
  height: 3rem;
  border: none;
  background: transparent;
  color: var(--gray-800);
  border-radius: 4px;
  font: 500 1.05rem var(--font-main);
  cursor: pointer;
  transition: background-color 0.2s ease, color 0.2s ease, transform 0.1s ease;
}

.key:hover {
  background: var(--gray-100);
  color: var(--gray-800);
}

.key:active {
  background: var(--gray-100);
  transform: scale(0.95);
}

.key.op { color: var(--gray-600); }
.key.mute { color: var(--gray-600); }
.key.eq {
  background: var(--gray-100);
  color: var(--gray-900);
}

.hint {
  margin: 0.7rem 0 0;
  font-size: 0.78rem;
  color: var(--gray-500);
}

.hist-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 28rem;
  overflow: auto;
}

.hist-list button {
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  color: inherit;
  font: inherit;
  padding: 0.45rem 0;
  cursor: pointer;
}

.hist-list em {
  font-style: normal;
  color: var(--gray-700);
  float: right;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: none;
  background: var(--gray-100);
  color: var(--gray-800);
  border-radius: 6px;
  padding: 0.55rem 0.95rem;
  font: 600 0.9rem var(--font-main);
  cursor: pointer;
}

.btn:hover {
  background: var(--gray-200);
  color: var(--gray-900);
}

.tts-banner {
  display: none;
  margin: 0;
  padding: 0.45rem 1rem;
  font-size: 0.8rem;
  background: var(--gray-50);
  color: var(--gray-800);
}

.tts-banner.is-on { display: block; }

#toast {
  position: fixed;
  right: 1.2rem;
  bottom: 1.2rem;
  z-index: 2000;
  background: var(--gray-50);
  color: var(--gray-900);
  padding: 0.55rem 0.9rem;
  border-radius: 6px;
  border: none;
  font-size: 0.85rem;
  box-shadow: var(--shadow-md);
}

dialog {
  border: none;
  border-radius: 6px;
  padding: 1.2rem 1.3rem;
  background: var(--white);
  color: var(--gray-900);
  max-width: 22rem;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.7);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

.dialog-actions button[value="cancel"] {
  background: none;
  border: none;
  color: var(--gray-700);
  border-radius: 6px;
  padding: 0.45rem 0.8rem;
  font: inherit;
  cursor: pointer;
}

.dialog-actions button[value="cancel"]:hover {
  background: var(--gray-50);
  color: var(--gray-800);
}

@media (max-width: 800px) {
  .shell {
    flex-direction: column;
    height: 100dvh;
    min-height: 100dvh;
    padding-bottom: 4.4rem;
    box-sizing: border-box;
  }

  .stage {
    min-height: 0;
    height: auto;
    flex: 1;
  }

  .rail {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: auto;
    height: 4.4rem;
    flex-direction: row;
    align-items: center;
    padding: 0 0.4rem;
    border-right: 0;
    background: var(--gray-50);
    z-index: 50;
  }

  .brand { display: none; }

  .nav {
    flex-direction: row;
    flex: 1;
    justify-content: space-around;
  }

  .nav-link {
    flex: 1;
    padding: 0.35rem 0.1rem;
    font-size: 0.58rem;
  }

  .topbar {
    height: 64px;
    padding: 0 var(--space-lg);
  }

  .view { padding: 1rem; }

  .home-grid,
  .calc-layout {
    grid-template-columns: 1fr;
  }

  body.is-reading-immersive {
    overflow: hidden;
  }

  body.is-reading-immersive .shell {
    padding-bottom: 0;
  }

  body.is-reading-immersive .topbar,
  body.is-reading-immersive .rail {
    display: none;
  }
}

@media (min-width: 1100px) {
  .rail {
    width: 13.5rem;
    align-items: stretch;
    padding: 1.2rem 0.85rem;
  }

  .brand {
    width: auto;
    justify-content: flex-start;
    padding: 0 0.7rem;
    gap: 0.55rem;
  }

  .brand-name {
    font-size: 0.92rem;
    font-weight: 600;
  }

  .nav-link {
    flex-direction: row;
    justify-content: flex-start;
    gap: 0.7rem;
    padding: 0.55rem 0.7rem;
    font-size: 0.88rem;
  }
}
