/* Sheetwright — app shell styles.
   Direction: fantasy guild hall meets a tidy spreadsheet. Parchment surfaces,
   ink text, one violet accent for actions and warm gold for XP. All colours
   are tokens on :root; no external fonts or images. */

:root {
  --paper: #f3ecdc;
  --paper-deep: #e7dcc3;
  --panel: #fffaf0;
  --panel-soft: #f8f1e1;
  --line: #d8ccb0;
  --line-strong: #b9a985;
  --ink: #2a2733;
  --muted: #655f70;
  --primary: #5a4a9c;
  --primary-deep: #46397c;
  --primary-soft: #e8e3f6;
  --on-primary: #ffffff;
  --gold: #c08a2b;
  --gold-soft: #f6e4b8;
  --gold-deep: #74500a;
  --ok: #276b4e;
  --ok-soft: #dff0e6;
  --lock: #625a70;
  --lock-soft: #ece8ee;
  --danger: #a8402f;
  --danger-soft: #f9e3df;
  --focus: #d9651e;
  --shadow: 0 1px 2px rgba(42, 39, 51, 0.08), 0 6px 18px rgba(42, 39, 51, 0.08);
  --radius: 14px;
  --radius-sm: 8px;
  --gutter: 16px;
  --content-max: 1080px;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Hiragino Sans",
    "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Yu Gothic", "Noto Sans JP", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Courier New", monospace;
}

*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100vh;
  /* A solid paper colour with one soft highlight at the top; the highlight
     fades to transparent, so the page looks the same at any height. */
  background: radial-gradient(1200px 520px at 50% -220px, var(--panel) 0%, transparent 70%) no-repeat,
    var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.65;
  overflow-wrap: anywhere;
}

a { color: var(--primary-deep); }
a:hover { color: var(--primary); }

h1, h2, h3, p, ul, ol { margin: 0; }

kbd {
  font-family: var(--font-sans);
  font-size: 0.92em;
  font-weight: 700;
  padding: 0.05em 0.45em;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  white-space: nowrap;
}

/* Focus: one obvious ring everywhere. */
:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

.skip-link {
  position: absolute;
  left: var(--gutter);
  top: -100px;
  z-index: 10;
  padding: 8px 12px;
  background: var(--panel);
  border: 2px solid var(--primary);
  border-radius: var(--radius-sm);
  font-weight: 700;
}
.skip-link:focus { top: 8px; }

/* ---- layout -------------------------------------------------------------- */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header,
.app-main,
.app-footer {
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  padding-left: var(--gutter);
  padding-right: var(--gutter);
}

.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-top: 8px;
  padding-bottom: 32px;
}
.app-main:focus { outline: none; }

/* ---- header -------------------------------------------------------------- */

.app-header {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "brand nav"
    "stats stats";
  gap: 12px 24px;
  align-items: center;
  padding-top: 18px;
  padding-bottom: 18px;
}

.brand {
  grid-area: brand;
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.logo {
  flex: none;
  display: grid;
  place-items: center;
  width: 56px;
  height: 56px;
  font-size: 30px;
  text-decoration: none;
  border-radius: 16px;
  background: linear-gradient(160deg, var(--gold-soft), var(--panel));
  border: 2px solid var(--gold);
  box-shadow: var(--shadow);
}

.brand-title {
  font-size: clamp(1.4rem, 3vw, 1.9rem);
  font-weight: 800;
  letter-spacing: 0.04em;
  line-height: 1.2;
}

.brand-subtitle {
  color: var(--muted);
  font-size: 0.92rem;
  margin-top: 2px;
}

.app-nav {
  grid-area: nav;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.app-nav a {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 999px;
  text-decoration: none;
  font-weight: 700;
  color: var(--primary-deep);
  background: var(--primary-soft);
}
.app-nav a:hover { background: var(--panel); box-shadow: inset 0 0 0 2px var(--primary); }

.stats {
  grid-area: stats;
  display: grid;
  grid-template-columns: auto auto 1fr;
  gap: 10px;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 14px;
  min-width: 0;
}

.stat-label {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
}

.stat-value {
  display: block;
  font-size: 1.1rem;
  font-weight: 800;
  line-height: 1.3;
}

.xp-bar {
  height: 10px;
  margin-top: 6px;
  border-radius: 999px;
  background: var(--paper-deep);
  border: 1px solid var(--line);
  overflow: hidden;
}

.xp-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--gold), var(--gold-soft));
  border-radius: inherit;
  transition: width 0.4s ease;
}

/* ---- cards & text ------------------------------------------------------------ */

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.screen-title {
  font-size: 1.3rem;
  font-weight: 800;
  line-height: 1.3;
}

.muted { color: var(--muted); font-size: 0.92rem; }
.lead { margin: 10px 0 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }

.notice,
.status-message {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
}
.notice {
  border: 1px solid var(--gold);
  background: var(--gold-soft);
  color: var(--gold-deep);
}
.status-message {
  border: 1px solid var(--ok);
  background: var(--ok-soft);
  color: var(--ok);
}

/* ---- buttons ------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 10px;
  border: 2px solid transparent;
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  line-height: 1.3;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.btn:hover { filter: brightness(1.05); }
.btn:active { transform: translateY(1px); }

.btn-primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary-deep); }
.btn-secondary { background: var(--primary-soft); color: var(--primary-deep); border-color: var(--primary-soft); }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--line); }
.btn-ghost:hover { color: var(--danger); border-color: var(--danger); }
.btn-danger { background: var(--danger); color: var(--on-primary); border-color: var(--danger); }

/* ---- home: reception -------------------------------------------------------- */

.reception {
  display: flex;
  gap: 18px;
  align-items: flex-start;
  background: linear-gradient(135deg, var(--panel) 0%, var(--panel-soft) 100%);
}

.npc {
  flex: none;
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  font-size: 40px;
  border-radius: 50%;
  background: var(--paper);
  border: 2px solid var(--line-strong);
}

.reception-body { min-width: 0; flex: 1; }

.npc-line {
  margin-top: 8px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--panel);
  border: 1px solid var(--line);
  position: relative;
  font-size: 1.05rem;
}
.npc-line::before {
  content: "";
  position: absolute;
  left: -9px;
  top: 16px;
  border: 8px solid transparent;
  border-right-color: var(--line);
  border-left: 0;
}

.npc-name {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 700;
}

.badges { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 0.9rem; }
.badge {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--gold-soft);
  border: 1px solid var(--gold);
  color: var(--gold-deep);
  font-weight: 700;
  font-size: 0.85rem;
}

/* ---- home: chapters ------------------------------------------------------------ */

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.chapter-stack { display: grid; gap: 14px; }

.upcoming { margin-top: 22px; }
.upcoming-title {
  font-size: 1rem;
  font-weight: 800;
  color: var(--muted);
  margin-bottom: 10px;
  padding-left: 2px;
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}

.chapter { display: flex; flex-direction: column; gap: 10px; padding: 16px; }

.chapter-playable {
  border: 2px solid var(--primary);
  box-shadow: var(--shadow), 0 0 0 4px var(--primary-soft);
}

.chapter-soon { background: var(--panel-soft); }
.chapter-soon .chapter-emoji { filter: grayscale(0.4); }

.chapter-head {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: start;
  gap: 12px;
}

.chapter-emoji { font-size: 32px; line-height: 1; }

.chapter-title { font-size: 1.1rem; font-weight: 800; line-height: 1.3; }
.chapter-theme { color: var(--muted); font-size: 0.88rem; }
.chapter-summary { font-size: 0.95rem; }

.pill {
  white-space: nowrap;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 800;
}
.pill-open { background: var(--primary-soft); color: var(--primary-deep); }
.pill-soon { background: var(--lock-soft); color: var(--lock); border: 1px dashed var(--lock); }

.quest-list {
  list-style: none;
  padding: 0;
  display: grid;
  gap: 8px;
}

.quest {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px 14px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--panel);
}

.quest-title { font-weight: 700; min-width: 0; }
.quest a.quest-title { text-decoration: none; color: var(--ink); }
.quest a.quest-title:hover { text-decoration: underline; color: var(--primary-deep); }

.quest-available { border-color: var(--primary); background: var(--primary-soft); }
.quest-cleared { background: var(--ok-soft); border-color: var(--ok); }
.quest-locked { color: var(--lock); background: var(--lock-soft); border-style: dashed; }

.quest-status { font-weight: 700; font-size: 0.9rem; white-space: nowrap; }
.status-available { color: var(--primary-deep); }
.status-cleared { color: var(--ok); }
.status-locked { color: var(--lock); }

.quest-xp {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--gold-deep);
  white-space: nowrap;
}

.home-tools { display: flex; flex-wrap: wrap; gap: 10px; justify-content: space-between; align-items: center; }

/* ---- how-to -------------------------------------------------------------------- */

.howto { display: flex; flex-direction: column; gap: 14px; }
.howto-section h3 { font-size: 1.05rem; font-weight: 800; margin-bottom: 6px; }
.howto-list { padding-left: 1.3em; display: grid; gap: 6px; }

/* ---- dialog ---------------------------------------------------------------------- */

.dialog {
  width: min(92vw, 420px);
  padding: 0;
  border: 2px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(42, 39, 51, 0.55); }
.dialog form { padding: 22px; }
.dialog-title { font-size: 1.15rem; font-weight: 800; }
.dialog-text { margin-top: 10px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ---- formula bar ------------------------------------------------------------------- */

.formula-bar {
  display: grid;
  grid-template-columns: auto auto 1fr;
  gap: 6px;
  align-items: stretch;
  margin-bottom: 10px;
}

.fb-address {
  display: grid;
  place-items: center;
  min-width: 84px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--paper-deep);
  font-family: var(--font-mono);
  font-weight: 700;
  white-space: nowrap;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.fb-fx {
  font: inherit;
  font-style: italic;
  font-weight: 800;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--primary-soft);
  background: var(--primary-soft);
  color: var(--primary-deep);
  cursor: pointer;
}
.fb-fx:hover { border-color: var(--primary); }

.fb-input {
  min-width: 0;
  padding: 8px 12px;
  font: inherit;
  font-family: var(--font-mono);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--panel);
  color: var(--ink);
}
.fb-input:read-only { background: var(--lock-soft); color: var(--lock); }

/* ---- sheet (grid) ------------------------------------------------------------------ */

.sheet-shell { display: flex; flex-direction: column; }

.sheet-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }

.btn-small { padding: 6px 12px; font-size: 0.92rem; border-radius: 8px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; filter: none; transform: none; }

/* Live regions stay in the accessibility tree while empty: they collapse
   to nothing instead of display: none. */
.sheet-alert,
.sheet-status { margin: 0 0 10px; }
.sheet-alert:empty,
.sheet-status:empty {
  height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  overflow: hidden;
}

.sheet-alert {
  padding: 10px 14px;
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 700;
}
.sheet-alert:not(:empty)::before { content: "⚠ "; }

.sheet-status { color: var(--muted); font-size: 0.9rem; }

.sheet-wrap {
  position: relative;
  overflow: auto;
  max-width: 100%;
  padding: 0 8px 10px 0; /* room for the fill handle at the edges */
}

.sheet {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  font-size: 0.95rem;
  line-height: 1.3;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
}

.sheet th,
.sheet td {
  height: 36px;
  padding: 0 8px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sheet tr > :last-child { border-right: 0; }
.sheet tbody tr:last-child > * { border-bottom: 0; }

.sheet th {
  position: sticky;
  z-index: 2;
  background: var(--paper-deep);
  color: var(--muted);
  font-weight: 700;
  font-size: 0.82rem;
  text-align: center;
  user-select: none;
}
.sheet thead th { top: 0; }
.sheet tbody th { left: 0; }
.sheet .corner { width: 44px; left: 0; z-index: 3; }
.sheet .col-head { width: 108px; }
.sheet th.is-selected { background: var(--primary-soft); color: var(--primary-deep); }

.cell {
  position: relative;
  background: var(--panel);
  color: var(--ink);
  cursor: cell;
  /* Keep scrollIntoView() clear of the sticky row / column headers. */
  scroll-margin-left: 44px;
  scroll-margin-top: 36px;
}
.cell.is-number { text-align: right; font-variant-numeric: tabular-nums; }
.cell.is-error { color: var(--danger); font-weight: 700; }

.cell.is-locked { background: var(--lock-soft); }
.cell-lock {
  display: none;
  position: absolute;
  right: 2px;
  top: 2px;
  font-size: 9px;
  line-height: 1;
  opacity: 0.8;
}
.cell.is-locked .cell-lock { display: block; }

.cell.is-selected { background: var(--primary-soft); }
.cell.is-locked.is-selected { background: color-mix(in srgb, var(--primary-soft) 55%, var(--lock-soft)); }
.cell.is-active { box-shadow: inset 0 0 0 2px var(--primary); }
.cell:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: -3px;
  border-radius: 0;
}

.cell.is-overlay { background: var(--gold-soft); box-shadow: inset 0 0 0 1px var(--gold); }
.cell.is-overlay-col { background: color-mix(in srgb, var(--gold) 38%, var(--gold-soft)); font-weight: 700; }
.cell.is-overlay.is-active { box-shadow: inset 0 0 0 2px var(--primary); }

.cell.is-pick { background: var(--ok-soft); box-shadow: inset 0 0 0 2px var(--ok); }

.cell.is-fill-preview { outline: 2px dashed var(--primary); outline-offset: -2px; }

.fill-handle {
  position: absolute;
  z-index: 3;
  width: 11px;
  height: 11px;
  border: 2px solid var(--panel);
  border-radius: 2px;
  background: var(--primary);
  cursor: crosshair;
  touch-action: none;
}

/* Column-number strip (overlay): its own row right above the range. */
.sheet .badge-row > td {
  height: 34px;
  padding: 0 4px;
  background: var(--gold-soft);
  border-bottom: 2px solid var(--gold);
  text-align: center;
  white-space: nowrap;
  overflow: visible;
}
.sheet .badge-corner {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--paper-deep);
}
.range-badge {
  display: inline-grid;
  place-items: center;
  min-width: 1.6em;
  height: 1.6em;
  padding: 0 0.2em;
  font-size: 1.2rem;
  font-weight: 800;
  line-height: 1;
  color: var(--gold-deep);
  background: var(--panel);
  border: 2px solid var(--gold);
  border-radius: 999px;
  vertical-align: middle;
}
.badge-cell.is-highlight .range-badge {
  color: var(--on-primary);
  background: var(--gold-deep);
  border-color: var(--gold-deep);
  box-shadow: 0 0 0 3px var(--panel), 0 0 0 5px var(--gold-deep);
}
.badge-label {
  margin-left: 6px;
  font-size: 0.8rem;
  font-weight: 800;
  color: var(--gold-deep);
  vertical-align: middle;
}
.badge-label::before { content: "← "; }

.cell-editor {
  position: absolute;
  z-index: 4;
  padding: 0 8px;
  font: inherit;
  font-size: 0.95rem;
  border: 2px solid var(--primary);
  border-radius: 0;
  background: var(--panel);
  color: var(--ink);
  outline: none;
  box-shadow: var(--shadow);
}

/* ---- quest screen ------------------------------------------------------------------ */

.quest-head { display: flex; flex-direction: column; gap: 6px; }
.quest-meta { color: var(--muted); font-size: 0.88rem; font-weight: 700; }
.quest-note { font-size: 0.92rem; }
.quest-cleared-note { color: var(--ok); font-weight: 700; }

/* Request and checklist side by side; stacked on narrow screens. */
.quest-brief {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(0, 2fr);
  gap: 20px;
  align-items: start;
}

.quest-npc {
  display: flex;
  gap: 18px;
  align-items: flex-start;
  background: linear-gradient(135deg, var(--panel) 0%, var(--panel-soft) 100%);
}

.quest-subtitle { font-size: 1rem; font-weight: 800; margin-bottom: 6px; }
.quest-objectives { margin-top: 14px; }
.quest-objectives ul { padding-left: 1.3em; display: grid; gap: 4px; font-size: 0.95rem; }

.cond-list { list-style: none; padding: 0; display: grid; gap: 8px; }
.cond {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: start;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--panel-soft);
  font-size: 0.95rem;
}
.cond-mark { line-height: 1.65; }
.cond-state { font-size: 0.82rem; font-weight: 800; white-space: nowrap; color: var(--muted); line-height: 1.65; }
.cond-met { border-color: var(--ok); background: var(--ok-soft); }
.cond-met .cond-state { color: var(--ok); }
.cond-unmet { border-color: var(--danger); background: var(--danger-soft); }
.cond-unmet .cond-state { color: var(--danger); }

.quest-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px 16px;
  margin-bottom: 12px;
}
.quest-actions-main,
.quest-actions-sheet { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.quest-sheet-tools .sheet-toolbar { margin-bottom: 0; }

.quest-hints { display: grid; gap: 8px; margin-bottom: 12px; }
.quest-hints:empty { display: none; }
.hint {
  padding: 10px 14px;
  border-left: 4px solid var(--gold);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--gold-soft);
  color: var(--ink);
  font-size: 0.95rem;
}
.hint-label { color: var(--gold-deep); }

.quest-sheet-status:empty { display: none; }

.quest-feedback-host:empty { display: none; }
.feedback-success { border: 2px solid var(--ok); box-shadow: var(--shadow), 0 0 0 4px var(--ok-soft); }
.feedback-fail { border: 2px solid var(--gold); box-shadow: var(--shadow), 0 0 0 4px var(--gold-soft); }
.feedback-title { font-size: 1.4rem; font-weight: 800; line-height: 1.3; }
.feedback-title:focus { outline: none; }
.feedback-xp { margin-top: 6px; font-size: 1.05rem; }
.feedback-xp strong { color: var(--gold-deep); }
.feedback-rank { margin-top: 4px; font-weight: 700; }
.feedback-heading { font-size: 1rem; font-weight: 800; margin: 16px 0 6px; }
.feedback-text { font-size: 0.97rem; }
.real-excel { padding-left: 1.4em; display: grid; gap: 6px; font-size: 0.95rem; }
.feedback-messages { margin-top: 10px; padding-left: 1.3em; display: grid; gap: 6px; font-size: 1rem; }
.feedback-messages + .feedback-text { margin-top: 12px; }

/* ---- glossary popover --------------------------------------------------------------- */

.glossary-term-btn {
  display: inline;
  padding: 0 1px;
  margin: 0;
  border: 0;
  background: none;
  font: inherit;
  color: var(--primary-deep);
  font-weight: 700;
  text-decoration: underline dotted;
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
  cursor: help;
  border-radius: 3px;
}
.glossary-term-btn:hover,
.glossary-term-btn[aria-expanded="true"] { background: var(--primary-soft); }

.glossary-popover {
  position: absolute;
  z-index: 30;
  width: min(380px, calc(100vw - 16px));
  padding: 14px 16px;
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow), 0 0 0 4px var(--primary-soft);
  font-size: 0.93rem;
}
.glossary-popover:focus { outline: none; }
.glossary-term { font-weight: 800; font-size: 1rem; color: var(--primary-deep); }
.glossary-meaning { margin-top: 6px; }
.glossary-row { margin-top: 6px; }
.glossary-row code { font-family: var(--font-mono); font-size: 0.9em; }
.glossary-label {
  display: inline-block;
  margin-right: 6px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--gold-soft);
  color: var(--gold-deep);
  font-size: 0.8rem;
  font-weight: 800;
}
.glossary-actions { margin-top: 10px; display: flex; justify-content: flex-end; }

/* ---- function picker (関数の挿入) --------------------------------------------------- */

.fn-picker { width: min(92vw, 520px); }
.fn-picker-body { padding: 22px; }

.fn-list {
  display: grid;
  gap: 6px;
  margin-top: 14px;
  max-height: 50vh;
  overflow: auto;
  padding: 4px;
  border-radius: var(--radius-sm);
}

.fn-option {
  display: grid;
  grid-template-columns: 7.5em 1fr;
  gap: 4px 12px;
  align-items: baseline;
  padding: 10px 12px;
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel-soft);
  cursor: pointer;
}
.fn-option:hover { border-color: var(--primary); }
.fn-option.is-current { border-color: var(--primary); background: var(--primary-soft); }
.fn-option-name { font-family: var(--font-mono); font-weight: 800; color: var(--primary-deep); }
.fn-option-summary { font-size: 0.92rem; color: var(--ink); }

/* ---- argument dialog (関数の引数) ----------------------------------------------------- */

/* Non-modal panel in the page flow right under the sheet, so the target cell
   and the table stay visible while the learner points at cells. */
.fn-dialog {
  position: static;
  display: block;
  width: 100%;
  max-width: none;
  margin: 12px 0 0;
  padding: 0;
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow), 0 0 0 4px var(--primary-soft);
}

.fn-dialog-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 14px;
  padding: 10px 18px;
  background: var(--primary-soft);
  border-bottom: 1px solid var(--line);
}
.fn-dialog-title { font-size: 1.05rem; font-weight: 800; color: var(--primary-deep); }
.fn-summary { font-size: 0.88rem; color: var(--muted); }

.fn-form { padding: 12px 18px 14px; }

.fn-fields { display: grid; gap: 8px; }

/* label | input | result on one row (the result "to the right of the field"). */
.fn-field {
  display: grid;
  grid-template-columns: 9em minmax(12em, 1fr) minmax(10em, 1fr);
  grid-template-areas: "label input result";
  gap: 2px 12px;
  align-items: center;
}
.fn-label { grid-area: label; font-weight: 700; font-size: 0.95rem; line-height: 1.3; }
.fn-optional { display: block; font-size: 0.78rem; font-weight: 600; color: var(--muted); }

.fn-input {
  grid-area: input;
  min-width: 0;
  padding: 7px 10px;
  font: inherit;
  font-family: var(--font-mono);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--panel);
  color: var(--ink);
}
.fn-input:focus { border-color: var(--primary); }

.fn-result {
  grid-area: result;
  min-height: 1.2em;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--ok);
  overflow-wrap: anywhere;
}
.fn-result[data-kind="pending"] { color: var(--muted); font-family: var(--font-sans); }
.fn-result[data-kind="range"] { color: var(--gold-deep); }

/* Result and help side by side under the fields. */
.fn-foot {
  display: grid;
  grid-template-columns: minmax(14em, 2fr) 3fr;
  gap: 12px;
  margin-top: 12px;
  align-items: stretch;
}

.fn-total {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--paper-deep);
  font-family: var(--font-mono);
  font-size: 1rem;
  align-self: start;
}
.fn-total-label { color: var(--muted); font-family: var(--font-sans); font-weight: 700; }
.fn-total strong[data-kind="incomplete"],
.fn-total strong[data-kind="pending"] { color: var(--muted); font-weight: 600; font-family: var(--font-sans); }

.fn-help-box {
  padding: 8px 12px;
  border-left: 4px solid var(--gold);
  background: var(--gold-soft);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.92rem;
  min-height: 3em;
}
.fn-help { color: var(--gold-deep); }

.fn-error {
  margin-top: 12px;
  padding: 10px 14px;
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-weight: 700;
}
.fn-error:empty { display: none; }
.fn-error:not(:empty)::before { content: "⚠ "; }

.fn-dialog .dialog-actions { margin-top: 12px; }

@media (hover: none) {
  .fill-handle { display: none; }
}

/* ---- footer -------------------------------------------------------------------- */

.app-footer {
  padding-top: 16px;
  padding-bottom: 24px;
  color: var(--muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--line);
}

/* ---- responsive ------------------------------------------------------------------ */

@media (max-width: 780px) {
  .app-header {
    grid-template-columns: 1fr;
    grid-template-areas:
      "brand"
      "nav"
      "stats";
  }
  .app-nav { justify-content: flex-start; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stat-bar { grid-column: 1 / -1; }
  .reception { flex-direction: column; }
  .npc-line::before { display: none; }
  .chapter-grid { grid-template-columns: 1fr; }
  .chapter-head { grid-template-columns: auto 1fr; }
  .chapter-head .pill { grid-column: 2; justify-self: start; }
  .quest { grid-template-columns: 1fr auto; }
  .quest-xp { grid-column: 2; justify-self: end; }
  .quest-brief { grid-template-columns: 1fr; }
  .quest-npc { flex-direction: column; }
  .quest-npc .npc-line::before { display: none; }
  .quest-actions { flex-direction: column; align-items: stretch; }
  .quest-actions-main .btn { flex: 1; justify-content: center; }
  .home-tools { flex-direction: column; align-items: stretch; }
  .home-tools .btn { justify-content: center; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions .btn { justify-content: center; }
  /* Bottom sheet: the grid above stays reachable. */
  .fn-dialog {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 20;
    width: auto;
    margin: 0;
    max-height: 60vh;
    overflow: auto;
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .fn-field { grid-template-columns: 1fr; grid-template-areas: "label" "input" "result"; }
  .fn-optional { display: inline; margin-left: 6px; }
  .fn-foot { grid-template-columns: 1fr; }
  .fn-option { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
