/* STM Study Hub — modern readable UI, dark/light themes */

:root {
  --font-sans: "DM Sans", system-ui, -apple-system, sans-serif;
  --font-display: "Fraunces", Georgia, "Times New Roman", serif;
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.12);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.18);
  --touch-min: 46px;
  --transition: 0.22s ease;
}

/* Dark (default) */
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0c0f14;
  --bg-elevated: #12161f;
  --surface: #181d28;
  --surface-2: #1e2533;
  --border: rgba(122, 142, 180, 0.22);
  --border-strong: rgba(122, 142, 180, 0.38);
  --text: #eef2f8;
  --text-muted: #9aa8bc;
  --accent: #8eb4ff;
  --accent-hover: #afc8ff;
  --accent-soft: rgba(142, 180, 255, 0.14);
  --accent-ring: rgba(142, 180, 255, 0.45);
  --grad-header: linear-gradient(180deg, #141924 0%, #0c0f14 100%);
  --grad-page: radial-gradient(120% 80% at 50% -20%, rgba(96, 132, 220, 0.12), transparent 55%),
    radial-gradient(80% 50% at 100% 50%, rgba(120, 100, 200, 0.06), transparent),
    var(--bg);
  --prose-heading: #fff;
  --card-front: #1a2030;
  --card-back: #151a27;
  --error: #f87171;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f0f2f6;
  --bg-elevated: #ffffff;
  --surface: #ffffff;
  --surface-2: #f4f6fb;
  --border: rgba(15, 23, 42, 0.1);
  --border-strong: rgba(15, 23, 42, 0.18);
  --text: #152030;
  --text-muted: #5c6b83;
  --accent: #3159c4;
  --accent-hover: #2347a8;
  --accent-soft: rgba(49, 89, 196, 0.1);
  --accent-ring: rgba(49, 89, 196, 0.35);
  --grad-header: linear-gradient(180deg, #ffffff 0%, #f0f2f6 100%);
  --grad-page: radial-gradient(120% 90% at 20% -10%, rgba(100, 130, 255, 0.1), transparent 50%),
    radial-gradient(70% 40% at 95% 20%, rgba(255, 180, 120, 0.08), transparent),
    var(--bg);
  --prose-heading: #0d1520;
  --card-front: #ffffff;
  --card-back: #f4f6fb;
  --error: #c62828;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font-sans);
  font-size: 1.02rem;
  line-height: 1.55;
  background: var(--grad-page);
  background-attachment: fixed;
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom, 0);
  transition: background-color var(--transition), color var(--transition);
}

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

/* Header */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--grad-header);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: max(0.65rem, env(safe-area-inset-top, 0)) 1rem 0.75rem;
  box-shadow: var(--shadow-sm);
}

.header-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.85rem;
}

.brand {
  font-family: var(--font-display);
  font-size: clamp(1.05rem, 3.5vw, 1.28rem);
  font-weight: 600;
  margin: 0;
  line-height: 1.25;
  letter-spacing: -0.02em;
  color: var(--prose-heading);
}

.brand-sub {
  display: block;
  font-family: var(--font-sans);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 0.3rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.theme-toggle {
  flex-shrink: 0;
  width: var(--touch-min);
  height: var(--touch-min);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), border-color var(--transition), transform 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.theme-toggle:hover {
  background: var(--accent-soft);
  border-color: var(--border-strong);
}

.theme-toggle:active {
  transform: scale(0.96);
}

.theme-toggle svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.theme-toggle .icon-sun {
  display: none;
}

html[data-theme="light"] .theme-toggle .icon-moon {
  display: none;
}

html[data-theme="light"] .theme-toggle .icon-sun {
  display: block;
}

.tabs {
  display: flex;
  gap: 0.5rem;
  padding: 0.2rem;
  background: var(--surface-2);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
}

.tab {
  flex: 1;
  min-height: var(--touch-min);
  font: inherit;
  font-weight: 600;
  font-size: 0.88rem;
  letter-spacing: 0.02em;
  padding: 0.45rem 0.85rem;
  border-radius: var(--radius-md);
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background var(--transition), color var(--transition), box-shadow var(--transition);
  -webkit-tap-highlight-color: transparent;
}

@media (min-width: 600px) {
  .tab {
    flex: 0 0 auto;
    min-width: 7.5rem;
  }
}

.tab[aria-selected="true"] {
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.panel.hidden {
  display: none !important;
}

/* Notes layout */
.layout {
  display: grid;
  grid-template-columns: 1fr;
  min-height: calc(100vh - 140px);
  gap: 0;
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(260px, 300px) 1fr;
    gap: 0 1.5rem;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.25rem;
  }
}

.sidebar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1rem 1.25rem;
  max-height: 44vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 900px) {
  .sidebar {
    margin-top: 1rem;
    border-bottom: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    max-height: none;
    min-height: calc(100vh - 130px);
    box-shadow: var(--shadow-md);
  }
}

.note-filter {
  width: 100%;
  min-height: var(--touch-min);
  padding: 0.6rem 0.9rem;
  margin-bottom: 0.85rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.note-filter:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.note-filter::placeholder {
  color: var(--text-muted);
}

.topic-nav {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.topic-nav button {
  text-align: left;
  font: inherit;
  font-size: 0.9rem;
  line-height: 1.35;
  min-height: var(--touch-min);
  padding: 0.55rem 0.75rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.topic-nav button:hover {
  background: var(--accent-soft);
  border-color: var(--border);
}

.topic-nav button.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

html[data-theme="light"] .topic-nav button.active {
  color: var(--accent-hover);
}

.topic-nav button.hidden-by-filter {
  display: none;
}

.main-content {
  padding: 1.1rem 1rem 3rem;
  overflow-x: auto;
  max-width: 42rem;
  margin: 0 auto;
  width: 100%;
}

@media (min-width: 900px) {
  .main-content {
    margin: 1rem 0 2rem;
    padding: 1.5rem 1.75rem 3rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
  }
}

.prose {
  font-size: 1.05rem;
  line-height: 1.72;
  color: var(--text);
  letter-spacing: 0.01em;
}

@media (max-width: 480px) {
  .prose {
    font-size: 1rem;
    line-height: 1.68;
  }
}

.prose p {
  margin: 0 0 1.1rem;
}

.prose h1 {
  font-family: var(--font-display);
  font-size: clamp(1.35rem, 4.5vw, 1.85rem);
  font-weight: 600;
  margin: 0 0 1rem;
  line-height: 1.22;
  letter-spacing: -0.03em;
  color: var(--prose-heading);
}

.prose h2 {
  font-family: var(--font-display);
  font-size: 1.18rem;
  font-weight: 600;
  margin: 1.75rem 0 0.75rem;
  letter-spacing: -0.02em;
  color: var(--prose-heading);
  padding-bottom: 0.35rem;
  border-bottom: 1px solid var(--border);
}

.prose ul, .prose ol {
  margin: 0 0 1.1rem;
  padding-left: 1.35rem;
}

.prose li {
  margin-bottom: 0.35rem;
}

.prose img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  margin: 1.15rem 0;
  box-shadow: var(--shadow-md);
}

.prose a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
}

.prose a:hover {
  color: var(--accent-hover);
}

.status {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-bottom: 0.85rem;
}

.error {
  color: var(--error);
}

/* Flashcards */
.flashcards-app {
  max-width: 26rem;
  margin: 0 auto;
  padding: 1.1rem 1rem calc(2rem + env(safe-area-inset-bottom, 0));
}

@media (min-width: 480px) {
  .flashcards-app {
    padding-top: 1.5rem;
  }
}

.fc-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-end;
  margin-bottom: 0.85rem;
}

.fc-toolbar label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  flex: 1 1 140px;
}

.fc-toolbar select {
  min-height: var(--touch-min);
  padding: 0.5rem 0.65rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  cursor: pointer;
}

.fc-toolbar select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

#fc-shuffle {
  min-height: var(--touch-min);
  padding: 0 1.1rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

#fc-shuffle:hover {
  background: var(--accent-soft);
  border-color: var(--border-strong);
}

.fc-progress {
  font-size: 0.88rem;
  color: var(--text-muted);
  margin: 0 0 0.65rem;
}

.fc-hint {
  display: block;
  text-align: center;
  font-size: 0.78rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 0.55rem;
}

.fc-scene {
  width: 100%;
  min-height: 228px;
  margin-bottom: 1rem;
  perspective: 1400px;
}

@media (min-width: 480px) {
  .fc-scene {
    min-height: 268px;
  }
}

.fc-flip-wrap {
  width: 100%;
  min-height: inherit;
  border: none;
  padding: 0;
  background: none;
  cursor: pointer;
  display: block;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
}

.fc-flip-wrap:focus-visible {
  outline: none;
}

.fc-flip-wrap:focus-visible .fc-inner {
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.fc-inner {
  position: relative;
  width: 100%;
  min-height: 228px;
  transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
  transform-style: preserve-3d;
  border-radius: var(--radius-lg);
}

@media (min-width: 480px) {
  .fc-inner {
    min-height: 268px;
  }
}

.fc-inner.is-flipped {
  transform: rotateY(180deg);
}

.fc-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.1rem;
  overflow-y: auto;
  background: var(--card-front);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 1.02rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: var(--shadow-md);
}

.fc-back {
  transform: rotateY(180deg);
  background: var(--card-back);
}

.fc-nav-btns {
  display: flex;
  gap: 0.75rem;
}

.fc-nav-btns button {
  flex: 1;
  min-height: var(--touch-min);
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.fc-nav-btns button:hover:not(:disabled) {
  background: var(--accent-soft);
}

.fc-nav-btns button:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.fc-nav-btns button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Read aloud (Web Speech API + Gemini TTS) */
.note-actions {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.65rem;
  margin-bottom: 0.85rem;
}

.note-actions-row {
  display: flex;
  align-items: center;
  gap: 0.65rem 0.85rem;
  flex-wrap: wrap;
}

.tts-label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.tts-select {
  min-width: 8.5rem;
  min-height: 2.5rem;
  padding: 0.35rem 0.6rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
}

.tts-select:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tts-key-details {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.5rem 0.75rem;
  background: var(--surface-2);
  font-size: 0.85rem;
}

.tts-key-summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
}

.tts-key-help {
  margin: 0.65rem 0;
  color: var(--text-muted);
  line-height: 1.5;
  font-size: 0.82rem;
}

.tts-key-help a {
  color: var(--accent);
}

.tts-key-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.tts-api-input {
  flex: 1 1 180px;
  min-width: 0;
  min-height: 2.5rem;
  padding: 0.45rem 0.65rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font: inherit;
  font-size: 0.88rem;
}

.tts-btn {
  min-height: 2rem;
  padding: 0.3rem 0.65rem;
  font: inherit;
  font-weight: 600;
  font-size: 0.8rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.tts-btn:hover {
  background: var(--accent-soft);
  border-color: var(--border-strong);
}

.tts-btn-small {
  min-height: 2rem;
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
}

.speak-btn {
  flex-shrink: 0;
  width: var(--touch-min);
  height: var(--touch-min);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), border-color var(--transition), transform 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}

.speak-btn:hover {
  background: var(--accent-soft);
  border-color: var(--border-strong);
}

.speak-btn:active {
  transform: scale(0.96);
}

.speak-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.speak-btn .icon-speak,
.speak-btn .icon-speak-stop {
  width: 24px;
  height: 24px;
}

.speak-btn .icon-speak-stop {
  display: none;
}

.speak-btn.is-speaking {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

html[data-theme="light"] .speak-btn.is-speaking {
  color: var(--accent-hover);
}

.speak-btn.is-speaking .icon-speak {
  display: none;
}

.speak-btn.is-speaking .icon-speak-stop {
  display: block;
}

.speak-hint {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.35;
  margin: 0;
}
