/* La Macchina Volante — living codex page */
@font-face {
  font-family: 'IM Fell';
  src: url('fonts/imfell-regular.woff2') format('woff2');
  font-weight: 400; font-style: normal; font-display: swap;
}
@font-face {
  font-family: 'IM Fell';
  src: url('fonts/imfell-italic.woff2') format('woff2');
  font-weight: 400; font-style: italic; font-display: swap;
}
@font-face {
  font-family: 'IM Fell SC';
  src: url('fonts/imfell-sc.woff2') format('woff2');
  font-weight: 400; font-style: normal; font-display: swap;
}

:root {
  --ink: #46311d;
  --ink-soft: #5d4426;
  --ink-faint: rgba(70, 49, 29, 0.55);
  --paper: #e9dcbd;
  --paper-hi: #f4ead0;
  --paper-lo: #d9c69c;
  --rust: #8a4b23;
  --shadow: rgba(58, 38, 16, 0.35);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; overflow: hidden; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: 'IM Fell', Georgia, 'Times New Roman', serif;
  -webkit-font-smoothing: antialiased;
}
#app { position: fixed; inset: 0; }
#gl { position: absolute; inset: 0; cursor: grab; touch-action: none; }
#gl.dragging { cursor: grabbing; }
#gl canvas { display: block; width: 100%; height: 100%; }

#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 4; }
#labels { position: absolute; inset: 0; pointer-events: none; z-index: 5; }

#grain {
  position: absolute; inset: 0; z-index: 6; pointer-events: none;
  opacity: 0.5; mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0.42 0 0 0 0 0.32 0 0 0 0 0.18 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E");
}
#vignette {
  position: absolute; inset: 0; z-index: 6; pointer-events: none;
  background:
    radial-gradient(ellipse 120% 90% at 50% 42%, transparent 58%, rgba(74, 51, 32, 0.16) 88%, rgba(60, 40, 20, 0.3) 100%);
}

/* ---- cartouche ---- */
#cartouche {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%);
  text-align: center; z-index: 8; pointer-events: none; width: min(92vw, 560px);
  transition: opacity 0.9s ease;
}
#cartouche h1 {
  font-family: 'IM Fell SC', 'IM Fell', serif; font-weight: 400;
  font-size: clamp(20px, 3.4vw, 34px); letter-spacing: 0.06em; color: var(--ink);
  text-shadow: 0 1px 0 rgba(244, 234, 208, 0.8);
}
#cartouche .sub {
  font-style: italic; font-size: clamp(11px, 1.5vw, 14px); color: var(--ink-soft);
  margin-top: 1px;
}
.cartouche-rule {
  height: 5px; margin: 3px auto; width: 78%;
  border-top: 1px solid var(--ink-faint); border-bottom: 1px solid var(--ink-faint);
}

/* ---- ink buttons ---- */
.ink-btn {
  font-family: 'IM Fell', serif; font-style: italic; font-size: 14px;
  color: var(--ink); background: linear-gradient(180deg, var(--paper-hi), var(--paper-lo));
  border: 1px solid var(--ink-soft); border-radius: 3px;
  padding: 7px 12px; cursor: pointer;
  box-shadow: 0 1px 2px var(--shadow), inset 0 1px 0 rgba(255,250,235,0.7);
  display: inline-flex; align-items: center; gap: 7px; line-height: 1;
  transition: transform .12s ease, box-shadow .12s ease, background .2s;
  min-height: 34px;
}
.ink-btn:hover { transform: translateY(-1px); box-shadow: 0 3px 6px var(--shadow), inset 0 1px 0 rgba(255,250,235,0.7); }
.ink-btn:active { transform: translateY(0.5px); box-shadow: 0 1px 1px var(--shadow); }
.ink-btn.active { background: linear-gradient(180deg, #e4d0a3, #cdb380); box-shadow: inset 0 2px 4px var(--shadow); }
.ink-btn .ic { width: 15px; height: 15px; fill: var(--ink); flex: none; }
.ink-btn .ic path[stroke] { fill: none; }

/* ---- toolbar ---- */
#toolbar {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; gap: 14px; align-items: stretch; z-index: 9; flex-wrap: wrap; justify-content: center;
  padding: 0 10px; max-width: 100vw;
}
.tool-group {
  display: flex; gap: 8px; align-items: center;
  background: rgba(233, 220, 189, 0.82); border: 1px solid var(--ink-faint);
  border-radius: 6px; padding: 7px 10px;
  box-shadow: 0 2px 10px rgba(58,38,16,0.22), inset 0 0 24px rgba(244,234,208,0.5);
  backdrop-filter: blur(2px);
}
.slider-wrap { display: flex; align-items: center; gap: 8px; }
.slider-tag { font-style: italic; font-size: 13px; color: var(--ink-soft); }
.slider-val { font-size: 12.5px; width: 40px; text-align: right; font-variant-numeric: tabular-nums; }
input[type="range"] {
  -webkit-appearance: none; appearance: none; width: 130px; height: 22px; background: transparent; cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px; background: linear-gradient(90deg, var(--ink-soft), #7d5f38);
  border-radius: 2px; box-shadow: inset 0 1px 1px rgba(0,0,0,0.25);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; margin-top: -6px;
  background: radial-gradient(circle at 35% 30%, #f4ead0, #c8ab72 70%, #a3824e);
  border: 1.5px solid var(--ink); border-radius: 50%;
  box-shadow: 0 1px 3px var(--shadow);
}
input[type="range"]::-moz-range-track {
  height: 4px; background: linear-gradient(90deg, var(--ink-soft), #7d5f38); border-radius: 2px;
}
input[type="range"]::-moz-range-thumb {
  width: 15px; height: 15px; background: radial-gradient(circle at 35% 30%, #f4ead0, #c8ab72 70%, #a3824e);
  border: 1.5px solid var(--ink); border-radius: 50%;
}

/* ---- chapter page ---- */
.codex-page {
  position: absolute; top: 50%; left: 18px; transform: translateY(-50%);
  width: min(300px, 78vw); z-index: 8;
  background:
    linear-gradient(105deg, rgba(244,234,208,0.97), rgba(226,209,165,0.97)),
    var(--paper);
  border: 1px solid var(--ink-soft); border-radius: 2px 10px 10px 2px;
  box-shadow: 0 6px 24px rgba(58,38,16,0.4), inset 0 0 40px rgba(190,160,105,0.35), inset 4px 0 8px rgba(58,38,16,0.15);
  padding: 18px 16px 12px 20px;
}
.codex-page::before {
  content: ""; position: absolute; left: 6px; top: 8px; bottom: 8px; width: 1px;
  background: repeating-linear-gradient(180deg, var(--ink-faint) 0 6px, transparent 6px 11px);
}
.folio-head { border-bottom: 1px solid var(--ink-faint); padding-bottom: 8px; margin-bottom: 8px; position: relative; }
.folio-num {
  position: absolute; right: 2px; top: 0; font-family: 'IM Fell SC', serif;
  font-size: 30px; color: var(--rust); opacity: 0.8;
}
#chapTitle { font-family: 'IM Fell SC', serif; font-weight: 400; font-size: 22px; letter-spacing: 0.03em; }
.folio-sub { font-style: italic; font-size: 12.5px; color: var(--ink-soft); }
.folio-text { font-size: 13.5px; line-height: 1.42; margin-bottom: 8px; }
.folio-text::first-letter { font-family: 'IM Fell SC', serif; font-size: 26px; line-height: 0.9; color: var(--rust); padding-right: 2px; }
.folio-notes { list-style: none; margin-bottom: 10px; }
.folio-notes li {
  font-style: italic; font-size: 12.5px; color: var(--ink-soft); padding: 2px 0 2px 14px; position: relative;
}
.folio-notes li::before { content: "\2726"; position: absolute; left: 0; font-size: 9px; top: 6px; color: var(--rust); }
.folio-nav { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--ink-faint); padding-top: 8px; }
.folio-nav .ink-btn { padding: 5px 9px; font-size: 12.5px; min-height: 28px; }
.dots { display: flex; gap: 5px; }
.dots i { width: 7px; height: 7px; border: 1px solid var(--ink-soft); border-radius: 50%; display: block; }
.dots i.on { background: var(--rust); border-color: var(--rust); }
.corner-x {
  position: absolute; top: 6px; right: 8px; border: none; background: none;
  font-family: inherit; font-size: 20px; color: var(--ink-soft); cursor: pointer; line-height: 1;
}
.corner-x:hover { color: var(--ink); }

/* ---- legend ---- */
#legend {
  position: absolute; right: 12px; bottom: max(14px, env(safe-area-inset-bottom));
  font-style: italic; font-size: 12px; color: var(--ink-soft); z-index: 8;
  background: rgba(233,220,189,0.7); padding: 6px 10px; border-radius: 5px;
  border: 1px solid var(--ink-faint); pointer-events: none; max-width: 46vw;
}
#legend b { font-style: normal; color: var(--ink); }
#legend .sep { opacity: 0.5; margin: 0 5px; }

/* ---- flight hud ---- */
#flightHud {
  position: absolute; top: 74px; left: 50%; transform: translateX(-50%); z-index: 9;
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
}
.fh-title { font-family: 'IM Fell SC', serif; font-size: 20px; letter-spacing: 0.08em; text-shadow: 0 1px 0 rgba(244,234,208,0.9); }
.fh-hint { font-style: italic; font-size: 12.5px; color: var(--ink-soft); }
#flightHud .ink-btn { font-size: 12.5px; }

/* ---- explode labels ---- */
.plate-label {
  position: absolute; transform: translate(-50%, -50%); pointer-events: none;
  font-style: italic; font-size: 12.5px; white-space: nowrap; color: var(--ink);
  background: linear-gradient(180deg, rgba(244,234,208,0.94), rgba(226,209,165,0.94));
  border: 1px solid var(--ink-soft); border-radius: 2px; padding: 2.5px 8px;
  box-shadow: 0 1px 4px rgba(58,38,16,0.3);
}
.plate-label .pl-num { font-family: 'IM Fell SC', serif; color: var(--rust); font-style: normal; margin-right: 5px; }
.plate-label.far { opacity: 0.0; }

/* ---- toast ---- */
#toast {
  position: absolute; bottom: 108px; left: 50%; transform: translateX(-50%); z-index: 10;
  font-style: italic; font-size: 14px; color: var(--paper-hi);
  background: rgba(70, 49, 29, 0.88); padding: 8px 16px; border-radius: 4px;
  box-shadow: 0 3px 10px rgba(0,0,0,0.3); pointer-events: none;
  transition: opacity .4s ease;
}

/* ---- veil ---- */
#veil {
  position: absolute; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(ellipse at 50% 40%, #f0e4c6 0%, #e0cda1 70%, #cfb887 100%);
  transition: opacity 0.9s ease; opacity: 1;
}
#veil.gone { opacity: 0; pointer-events: none; }
.veil-inner { text-align: center; }
.veil-mark { width: 130px; opacity: 0.85; }
.veil-title { font-family: 'IM Fell SC', serif; font-size: 26px; letter-spacing: 0.08em; margin-top: 8px; }
.veil-sub { font-style: italic; font-size: 13px; color: var(--ink-soft); margin: 4px 0 14px; }
.veil-bar { width: 190px; height: 3px; margin: 0 auto; background: rgba(70,49,29,0.25); border-radius: 2px; overflow: hidden; }
#veilFill { width: 0%; height: 100%; background: var(--ink-soft); transition: width .3s ease; }

/* ---- responsive ---- */
@media (max-width: 760px) {
  #cartouche { top: 8px; }
  #cartouche .sub { display: none; }
  .codex-page {
    top: auto; bottom: 86px; left: 50%; transform: translateX(-50%);
    width: min(340px, 92vw); max-height: 42vh; overflow-y: auto;
  }
  #legend { display: none; }
  .ink-btn { font-size: 13px; padding: 8px 10px; min-height: 40px; }
  .ink-btn span { display: none; }
  .ink-btn .ic { width: 18px; height: 18px; }
  #mechPlay span, #explodeBtn span { display: none; }
  input[type="range"] { width: 92px; }
  .slider-val { width: 34px; font-size: 11.5px; }
  #toolbar { gap: 8px; bottom: max(10px, env(safe-area-inset-bottom)); }
  .tool-group { padding: 6px 8px; gap: 6px; }
  #flightHud { top: 58px; }
  .plate-label { font-size: 11px; padding: 2px 6px; }
}
