/*
 * Tools-Portal — shared tool chrome (Phase 2).
 * Linked from every tool via ../assets/css/tool-chrome.css
 * Tool-specific palette/widgets stay in <tool>/style.css below the marker.
 *
 * Contract: LAYOUT-CONTRACT.md + APP-SHELLS.md
 * Do not fork masthead class names or parallel button systems.
 */

:root {
  --paper: #f5f1e8;
  --paper-raised: #fffdf7;
  --ink: #1c1a15;
  --ink-soft: #5a5646;
  --ink-faint: #8a8570;
  --rule: #d8d0bc;
  --rule-strong: #b9ae8c;
  --accent: #b5482f;
  --accent-soft: #f0ded7;
  --focus: #1c1a15;
  --radius-sm: 3px;
  --radius-md: 6px;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --font-mono: "SFMono-Regular", "JetBrains Mono", ui-monospace, "Cascadia Mono", monospace;
  /* Header controls (theme/lang/text-scale buttons) keep a constant physical
     footprint across text-scale steps — dividing by the same factor that
     grows the html font-size cancels it out, so the mobile masthead row
     doesn't overflow at high zoom (rem body text still scales normally). */
  --ctrl-h: calc(2.1rem / var(--text-scale-factor, 1));
  --shadow-card: 0 1px 3px rgba(28, 26, 21, 0.06);
}

html[data-theme="dark"] {
  --paper: #1b1914;
  --paper-raised: #262319;
  --ink: #ece6d6;
  --ink-soft: #b7ae95;
  --ink-faint: #8f8870;
  --rule: #3a362a;
  --rule-strong: #55503e;
  --accent: #e2825f;
  --accent-soft: #3a2620;
  --focus: #ece6d6;
  --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }

/* Global text-scale (pt-text-scale, see assets/js/text-scale.js). The JS sets
   --text-scale-factor as an inline style on <html> before first paint; the
   var() fallback here covers no-JS. rem-based sizing across the chrome/tool
   CSS scales automatically because it multiplies the html font-size. */
html { font-size: calc(100% * var(--text-scale-factor, 1)); }
html { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }

body {
  margin: 0;
  background: var(--paper);
  background-image: linear-gradient(var(--rule) 1px, transparent 1px);
  background-size: 100% 2.6rem;
  color: var(--ink);
  font-family: var(--font-serif);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

html[lang="th"] body,
html[lang="th"] .study-tile,
html[lang="th"] .study-tile__glyph {
  font-family: var(--font-thai), var(--font-serif);
  line-height: 1.8;
}

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

/* Author CSS often sets .modal/.drawer { display: flex } for the open state.
   That beats the UA `[hidden] { display: none }` mapping, so dialogs stay
   visible (and “won’t close”) when only the hidden attribute is toggled.
   Force hidden to always win — same fix thai-reading / pre-migration tools. */
[hidden] {
  display: none !important;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 100;
  padding: 0.5rem 1rem;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  text-decoration: none;
}

.skip-link:focus {
  left: 0.5rem;
  top: 0.5rem;
}

/* ---------- Page rail ---------- */
.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 2.5rem 1.5rem 5rem;
}

/* ---------- Masthead ---------- */
.masthead { padding-bottom: 1.15rem; }

.masthead-rule {
  border: none;
  border-top: 2px solid var(--ink);
  margin: 0 0 2.5rem;
}

.masthead-rule--toolbar {
  margin-bottom: 0.75rem;
}

.masthead-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.masthead-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.home-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  flex-shrink: 0;
  border-radius: 50%;
  color: var(--ink-soft);
  text-decoration: none;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.home-icon:hover {
  background: var(--accent-soft);
  color: var(--ink);
}

.home-icon:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.brand-tile {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: var(--ink);
  color: var(--paper);
  font-size: 1.05rem;
}

.brand-tile img {
  display: block;
  width: 1.25rem;
  height: 1.25rem;
  filter: brightness(0) invert(1);
}

html[data-theme="dark"] .brand-tile img {
  filter: brightness(0);
}

.brand-title {
  font-size: clamp(1.3rem, 3.2vw, 1.6rem);
  margin: 0;
  min-width: 0;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-title-sub {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-weight: 500;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.lang-badges {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.lang-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  height: var(--ctrl-h);
  min-height: var(--ctrl-h);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  border: 1px solid var(--rule-strong);
  border-radius: 999px;
  padding: 0 0.75rem;
  background: var(--paper-raised);
  color: var(--ink-soft);
  cursor: pointer;
  flex-shrink: 0;
  line-height: 1;
  transition: transform 0.1s ease, background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.lang-badge:hover { border-color: var(--ink); color: var(--ink); }
.lang-badge:active { transform: scale(0.96); }
.lang-badge:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.lang-badge.is-active {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.masthead-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.masthead-actions-end {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.theme-toggle {
  position: relative;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  min-height: var(--ctrl-h);
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--rule-strong);
  border-radius: 50%;
  background: var(--paper-raised);
  color: var(--ink-soft);
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, transform 0.1s ease;
}

/* Extra masthead actions (Add / Lessons / …) — same height & ink as lang/theme. */
.masthead-actions .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  box-sizing: border-box;
  height: var(--ctrl-h);
  min-height: var(--ctrl-h);
  padding: 0 0.7rem;
  border: 1px solid var(--rule-strong);
  border-radius: 999px;
  background: var(--paper-raised);
  color: var(--ink-soft);
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1;
  transition: border-color 0.12s ease, color 0.12s ease, background-color 0.12s ease;
}

.masthead-actions .icon-btn:hover {
  border-color: var(--ink);
  color: var(--ink);
}

.masthead-actions .icon-btn:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.masthead-actions .icon-btn svg,
.masthead-actions .icon-btn .icon {
  width: 1rem;
  height: 1rem;
  flex-shrink: 0;
}

.theme-toggle:hover { border-color: var(--ink); color: var(--ink); }
.theme-toggle:active { transform: scale(0.94); }
.theme-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.theme-icon {
  position: absolute;
  width: 1.1rem;
  height: 1.1rem;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.theme-icon--sun { opacity: 1; transform: scale(1) rotate(0deg); }
.theme-icon--moon { opacity: 0; transform: scale(0.5) rotate(-40deg); }
html[data-theme="dark"] .theme-icon--sun { opacity: 0; transform: scale(0.5) rotate(40deg); }
html[data-theme="dark"] .theme-icon--moon { opacity: 1; transform: scale(1) rotate(0deg); }

/* Text-scale group (A-/A+), same visual language as .theme-toggle. Wires up
   via assets/js/text-scale.js (localStorage key "pt-text-scale"). */
.text-scale-group {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  flex-shrink: 0;
}

.text-scale-btn {
  position: relative;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  min-height: var(--ctrl-h);
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--rule-strong);
  border-radius: 50%;
  background: var(--paper-raised);
  color: var(--ink-soft);
  cursor: pointer;
  font: inherit;
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1;
  transition: border-color 0.12s ease, color 0.12s ease, transform 0.1s ease;
}

.text-scale-btn:hover { border-color: var(--ink); color: var(--ink); }
.text-scale-btn:active { transform: scale(0.94); }
.text-scale-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.text-scale-btn:disabled { opacity: 0.4; cursor: default; transform: none; }

/* Portal-wide config dialog (gear icon, last header action). Holds theme +
   text-scale (and per-tool settings like zoom via [data-config-section]).
   Wires up via assets/js/config-dialog.js. Reusable across index + tools. */
.config-toggle {
  position: relative;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  min-height: var(--ctrl-h);
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--rule-strong);
  border-radius: 50%;
  background: var(--paper-raised);
  color: var(--ink-soft);
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, transform 0.1s ease;
}

.config-toggle:hover { border-color: var(--ink); color: var(--ink); }
.config-toggle:active { transform: scale(0.94); }
.config-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.config-toggle[aria-expanded="true"] { border-color: var(--ink); color: var(--ink); background: var(--paper); }

.config-icon { width: 1.1rem; height: 1.1rem; }

.config-dialog {
  border: none;
  padding: 0;
  width: min(22rem, calc(100vw - 2rem));
  max-height: min(85vh, 34rem);
  background: var(--paper-raised);
  border-radius: var(--radius-md);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.22);
  color: var(--ink);
}

.config-dialog[open] { animation: config-dialog-pop-in 0.16s ease both; }

@keyframes config-dialog-pop-in {
  from { opacity: 0; transform: translateY(-4px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.config-dialog::backdrop { background: rgba(27, 25, 20, 0.4); }

.config-dialog__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.1rem 0.5rem;
  border-bottom: 1px solid var(--rule);
}

.config-dialog__title { margin: 0; font-size: 1rem; }

.config-dialog__close {
  flex: 0 0 auto;
  width: var(--ctrl-h);
  height: var(--ctrl-h);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
}

.config-dialog__close:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.config-dialog__body {
  padding: 0.85rem 1.1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow: auto;
}

.config-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.config-section__label { font-size: 0.85rem; color: var(--ink-soft); }

@media (prefers-reduced-motion: reduce) {
  .config-toggle,
  .config-dialog[open] { transition: none; animation: none; }
}

/* Mobile: the native <dialog> UA default (centered, fixed) still applies,
   but at narrow widths widen it to the full safe-area and bump every touch
   target inside it to >=44px (Apple HIG / Material minimum) regardless of
   --ctrl-h, which intentionally stays small/constant for the desktop
   masthead row. Covers installed-PWA + mobile-Safari/Chrome alike — no
   browser-specific branching needed since this is plain CSS. Kept in sync
   with the same block in assets/css/portal.css. */
@media (max-width: 480px) {
  .config-dialog {
    width: calc(100vw - 1.5rem);
    max-height: min(88vh, 34rem);
  }

  .config-dialog__close,
  .theme-toggle,
  .text-scale-btn {
    width: max(2.75rem, var(--ctrl-h));
    height: max(2.75rem, var(--ctrl-h));
    min-height: 2.75rem;
    min-width: 2.75rem;
  }

  .config-section {
    flex-wrap: wrap;
    row-gap: 0.5rem;
  }
}

.masthead-sub {
  font-size: 1.05rem;
  color: var(--ink-soft);
  margin: 0.6rem 0 0;
}

.masthead-sub--tagline {
  max-width: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: clamp(0.72rem, 1.85vw, 1.05rem);
  line-height: 1.35;
}

/* ---------- App toolbar (shell: toolbar-workspace, study-trainer) ---------- */
.app-toolbar { margin-bottom: 0.65rem; }

.app-toolbar__row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
}

.app-toolbar__menus {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  flex: 1;
  min-width: 0;
  justify-content: flex-start;
}

.app-toolbar__actions {
  display: flex;
  gap: 0.35rem;
  flex-shrink: 0;
  margin-left: auto;
  justify-content: flex-end;
}

.toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: var(--ctrl-h);
  min-height: var(--ctrl-h);
  box-sizing: border-box;
  padding: 0 0.65rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--rule-strong);
  background: var(--paper-raised);
  color: var(--ink-soft);
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.72rem;
  letter-spacing: 0.03em;
  line-height: 1;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease, border-color 0.12s ease, transform 0.1s ease;
}

.toolbar-btn:hover { border-color: var(--ink); color: var(--ink); }
.toolbar-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.toolbar-btn:active { transform: scale(0.98); }
.toolbar-btn.is-active,
.tab-btn.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.toolbar-btn--icon {
  width: var(--ctrl-h);
  padding: 0;
  min-width: var(--ctrl-h);
}

.toolbar-btn--icon svg {
  width: 1rem;
  height: 1rem;
  flex-shrink: 0;
}

.tab-count {
  font-weight: 500;
  opacity: 0.8;
  font-size: 0.85em;
}

.hint-panel { margin-top: 0.55rem; }

.hint-copy {
  margin: 0;
  font-size: 0.8rem;
  color: var(--ink-faint);
  line-height: 1.45;
}

.hint-copy + .hint-copy {
  margin-top: 0.4rem;
}

.hint-toggle[aria-expanded="true"],
.toolbar-btn[aria-pressed="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.hint-toggle-mark {
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 700;
  font-size: 0.85rem;
  line-height: 1;
}

/* ---------- Info callout (standard (i) disclosure, collapsed by default) ----------
   Use for instructional copy outside the toolbar. Toolbar keeps .hint-toggle /
   .hint-panel; content areas use .info-callout. Wire with assets/js/info-callout.js
   or the same aria-expanded toggle pattern.

   Markup:
   <div class="info-callout">
     <button type="button" class="info-callout__toggle" aria-expanded="false"
             aria-controls="info-xyz" title="Info">
       <span class="info-callout__mark" aria-hidden="true">i</span>
       <span class="info-callout__label">Info</span>
     </button>
     <div class="info-callout__panel" id="info-xyz" hidden>
       <p class="info-callout__text">…</p>
     </div>
   </div>
*/
.info-callout {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.45rem;
  margin: 0 0 1rem;
}

.info-callout__toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  min-height: var(--ctrl-h);
  padding: 0 0.7rem 0 0.35rem;
  border: 1px solid var(--rule-strong);
  border-radius: 999px;
  background: var(--paper-raised);
  color: var(--ink-soft);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}

.info-callout__toggle:hover {
  border-color: var(--ink);
  color: var(--ink);
}

.info-callout__toggle:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.info-callout__toggle[aria-expanded="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.info-callout__mark {
  display: grid;
  place-items: center;
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 50%;
  border: 1.5px solid currentColor;
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 700;
  font-size: 0.78rem;
  line-height: 1;
  flex-shrink: 0;
}

.info-callout__label {
  padding-right: 0.15rem;
}

.info-callout__panel {
  width: 100%;
  max-width: 42rem;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  background: var(--paper-raised);
}

.info-callout__text {
  margin: 0;
  font-size: 0.88rem;
  line-height: 1.5;
  color: var(--ink-soft);
}

.info-callout__text + .info-callout__text {
  margin-top: 0.55rem;
}

/* ---------- Workspace (default main) ---------- */
.workspace {
  min-width: 0;
}

/* ---------- Shell: preview-detail ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1.75rem;
}

@media (min-width: 1024px) {
  .layout {
    grid-template-columns: minmax(0, 460px) minmax(0, 1fr);
    gap: 2.5rem;
  }

  .layout--form-left #tool-form,
  .layout--form-left form[id] { grid-column: 1; grid-row: 1; }
  .layout--form-left .preview-aside { grid-column: 2; grid-row: 1; }
}

.preview-aside { min-width: 0; }

@media (max-width: 1023px) {
  .preview-aside { display: contents; }
}

@media (min-width: 1024px) {
  .preview-sticky {
    position: sticky;
    top: 1.25rem;
  }
}

@media (max-width: 1023px) {
  .tool-section {
    scroll-margin-top: calc(var(--preview-dock-h, 14rem) + 0.5rem);
  }

  .preview-sticky {
    position: sticky;
    top: 0;
    z-index: 30;
    background: var(--paper);
    margin-inline: -1.5rem;
    padding: 0.35rem 1.5rem 0.5rem;
    border-bottom: 1px solid var(--rule);
  }
}

.preview-jump {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.45rem;
  margin-top: 0.45rem;
}

.preview-jump__link {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-decoration: none;
  color: var(--ink-soft);
  border: 1px solid var(--rule-strong);
  border-radius: 999px;
  padding: 0.3rem 0.65rem;
  transition: border-color 0.12s ease, color 0.12s ease, background-color 0.12s ease;
}

.preview-jump__link:hover { border-color: var(--ink); color: var(--ink); }
.preview-jump__link:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Shell: search-catalog ---------- */
.controls {
  margin: 0 0 2rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.search-input {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  padding: 0.75rem 1rem;
  background: var(--paper-raised);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
  width: 100%;
  max-width: 420px;
}

.search-input:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.category-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  background: transparent;
  border: 1px solid var(--rule-strong);
  border-radius: 999px;
  padding: 0.4rem 0.85rem;
  color: var(--ink-soft);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease, background-color 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}

.chip:hover { border-color: var(--ink); color: var(--ink); }
.chip:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.chip:active { transform: scale(0.97); }
.chip-active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

a.chip { text-decoration: none; }

/* search-catalog filter panel (PAT-ui-009). Chips stay for portal/osint. */
.filter-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  align-items: stretch;
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
  gap: 0.75rem 1rem;
}

.filter-grid .field {
  min-width: 0;
}

.filter-panel .reset-btn {
  align-self: flex-start;
}

.meta-line {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--ink-faint);
}

.reset-btn {
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  background: transparent;
  border: 0;
  padding: 0.35rem 0;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.reset-btn:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

@media (max-width: 640px) {
  .filter-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 420px) and (max-width: 640px) {
  .filter-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* ---------- Shell: study-trainer (tile grid like alphabet) ---------- */
.study-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr));
  gap: 0.7rem;
}

.study-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-height: 6.5rem;
  padding: 1rem 0.5rem 0.75rem;
  background: var(--paper-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-card);
  text-align: center;
  line-height: 1.6;
  overflow: visible;
  transition: border-color 0.15s ease, transform 0.12s ease, box-shadow 0.15s ease;
}

.study-tile:hover,
.study-tile:focus-visible {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 4px 14px -6px rgba(28, 26, 21, 0.2);
}

.study-tile:active { transform: translateY(0) scale(0.98); }

.study-tile.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

.study-tile__glyph {
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.5;
  overflow: visible;
}

.study-tile__label {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.study-tile__meta {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

/* Alphabet apps may alias: .char-grid { @apply study-grid } via duplicate selectors in tool CSS */

/* ---------- Form primitives ---------- */
fieldset {
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: 1rem 1.1rem 1.25rem;
  margin: 0 0 1rem;
  background: var(--paper-raised);
  min-width: 0;
}

legend {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  padding: 0 0.4rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 0.85rem;
}

.field:last-child { margin-bottom: 0; }

.field label,
.field-label {
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.field-label {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
}

.field input[type="text"],
.field input[type="search"],
.field input[type="date"],
.field input[type="time"],
.field select,
.field textarea {
  font-family: var(--font-serif);
  font-size: 0.95rem;
  padding: 0.55rem 0.7rem;
  background: var(--paper-raised);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  color: var(--ink);
  width: 100%;
}

.field input:focus-visible,
.field select:focus-visible,
.field textarea:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  letter-spacing: 0.02em;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.85rem;
  background: var(--paper-raised);
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.1s ease, background-color 0.12s ease, border-color 0.12s ease;
  white-space: nowrap;
}

.btn:hover { border-color: var(--ink); }
.btn:active { transform: scale(0.97); }
.btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.btn--primary { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn--primary:hover { background: #000; }
html[data-theme="dark"] .btn--primary:hover { background: #f5f1e8; color: #1b1914; }
.btn--sm { font-size: 0.72rem; padding: 0.35rem 0.65rem; }

/* ---------- Footer ---------- */
.page-footer {
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--rule);
  text-align: center;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--ink-faint);
}

.page-footer a,
.footer-legal a {
  color: var(--ink-soft);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.page-footer a:hover,
.footer-legal a:hover {
  color: var(--accent);
}

.footer-legal {
  margin-top: 0.5rem;
}

.app-version {
  margin-top: 0.35rem;
  opacity: 0.6;
}

.app-version:empty { display: none; }

/* ---------- Motion & mobile ---------- */
@media (prefers-reduced-motion: reduce) {
  .home-icon,
  .lang-badge,
  .theme-toggle,
  .theme-icon,
  .text-scale-btn,
  .config-toggle,
  .toolbar-btn,
  .preview-jump__link,
  .chip,
  .study-tile,
  .btn {
    transition: none;
  }
}

/* Header mobile (< 550px) — same as portal index: --ctrl-h stays 2.1rem,
   no vertical stretch. Brand + actions share one row when they fit;
   otherwise the action cluster wraps as one compact button line. */
@media (max-width: 549px) {
  .page { padding: 2rem 1rem 4rem; }
  .masthead-rule { margin-bottom: 1.5rem; }
  .masthead-rule--toolbar { margin-bottom: 0.75rem; }

  .masthead-row {
    align-items: center;
    gap: 0.5rem;
  }

  .masthead-brand {
    min-width: 0;
    flex: 1 1 auto;
  }

  .masthead-actions {
    flex: 0 1 auto;
    width: auto;
    margin-left: auto;
    justify-content: flex-end;
    flex-wrap: nowrap;
  }

  .masthead-actions-end,
  .lang-badges {
    flex-wrap: nowrap;
    align-items: center;
  }

  .masthead-actions .icon-btn {
    width: var(--ctrl-h);
    padding: 0;
    gap: 0;
    border-radius: 50%;
  }

  .masthead-actions .icon-btn-label,
  .masthead-actions .plugin-btn-label {
    display: none;
  }
}

/* Content / toolbar wrap — not header control sizes */
@media (max-width: 500px) {
  .app-toolbar__menus {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .app-toolbar__row { flex-wrap: wrap; }

  .search-input { max-width: none; }

  .category-filters {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.45rem;
  }

  .category-filters .chip { width: 100%; }

  fieldset { padding: 0.85rem 0.9rem 1rem; }

  .study-grid {
    grid-template-columns: repeat(auto-fill, minmax(5.5rem, 1fr));
    gap: 0.55rem;
  }
}
