:root {
  color-scheme: light;
  --paper: #e9e5da;
  --paper-2: #dcd7ca;
  --ink: #242521;
  --quiet: #74756e;
  --line: rgba(36, 37, 33, 0.2);
  --void: #101211;
  --glow: #dfa84e;
  --danger: #9c4b43;
  --door-x: 0%;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; margin: 0; background: #cac5b9; color: var(--ink); }

body { min-width: 300px; overscroll-behavior: none; }

button { font: inherit; color: inherit; }

button:focus-visible, [tabindex]:focus-visible { outline: 2px solid var(--danger); outline-offset: 3px; }

.app {
  width: min(100%, 520px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(250px, 45dvh) minmax(260px, 1fr);
  background: var(--paper);
  box-shadow: 0 0 48px rgba(28, 27, 23, 0.22);
  overflow: hidden;
}

.topbar {
  min-height: calc(62px + var(--safe-top));
  padding: calc(10px + var(--safe-top)) 12px 10px;
  display: grid;
  grid-template-columns: 40px 1fr auto 38px;
  gap: 9px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: #efede5;
  z-index: 20;
}

.icon-button {
  width: 38px; height: 38px; border: 1px solid var(--line); border-radius: 5px;
  background: rgba(255,255,255,.22); cursor: pointer; font-size: 19px;
}

.icon-button:active { background: rgba(30,30,25,.08); }
.sound-button { font-size: 17px; }
.title-lockup h1 { margin: 0; font: 650 15px/1.1 ui-monospace, SFMono-Regular, Consolas, monospace; letter-spacing: .08em; text-transform: uppercase; }
.title-lockup span { color: var(--quiet); font: 10px/1.2 ui-monospace, monospace; letter-spacing: .14em; }

.resources { display: flex; gap: 13px; text-align: right; }
.resources span { display: grid; }
.resources small { color: var(--quiet); font: 8px/1.2 ui-monospace, monospace; letter-spacing: .13em; }
.resources strong { font: 600 16px/1.1 ui-monospace, monospace; }

.encounter { min-height: 0; display: grid; grid-template-rows: 1fr 34px; border-bottom: 1px solid var(--line); }
.chamber { position: relative; min-height: 0; overflow: hidden; background: radial-gradient(circle at 60% 45%, #232420 0, #111312 55%, #090a09 100%); isolation: isolate; }
.chamber-noise { position: absolute; inset: 0; opacity: .14; background-image: repeating-linear-gradient(111deg, transparent 0 13px, rgba(255,255,255,.025) 14px 15px), repeating-linear-gradient(4deg, transparent 0 31px, rgba(255,255,255,.02) 32px); }
.chamber::after { content: ""; position: absolute; inset: 0; pointer-events: none; box-shadow: inset 0 0 55px #000; z-index: 6; }

.demon { position: absolute; width: min(74%, 330px); height: 82%; right: 2%; top: 8%; overflow: visible; color: var(--glow); filter: drop-shadow(0 0 8px color-mix(in srgb, var(--glow) 50%, transparent)); transition: opacity .3s, filter .3s, transform .45s cubic-bezier(.16,.8,.25,1); }
.demon .d-line { fill: none; stroke: currentColor; stroke-width: 3.3; stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.demon .d-fill { fill: color-mix(in srgb, currentColor 9%, transparent); stroke: currentColor; stroke-width: 2.2; vector-effect: non-scaling-stroke; }
.demon .fine { stroke-width: 1.2; opacity: .68; }
.demon.hurt { animation: impact .28s ease; }
.demon.defeated { opacity: 0; transform: scale(.72) translateY(20px); filter: blur(5px); }
.demon.banished { opacity: 0; transform: scale(.05) rotate(-12deg); filter: blur(3px); }
.demon.sealed { opacity: 0; transform: scaleX(.02); filter: blur(1px); }

@keyframes impact { 35% { transform: translateX(-7px); filter: brightness(1.8) drop-shadow(0 0 15px currentColor); } 70% { transform: translateX(4px); } }

.entity-stack { position: absolute; right: 7%; top: 25%; z-index: -1; display: flex; gap: 2px; opacity: .22; color: var(--glow); }
.entity-stack i { width: 40px; height: 85px; border: 1px solid currentColor; border-radius: 50% 50% 42% 42%; transform: translateX(calc(var(--i) * -20px)) scale(calc(1 - var(--i) * .08)); }

.target-readout { position: absolute; z-index: 7; right: 18px; bottom: 12px; color: #e3ded1; text-align: right; opacity: 0; transform: translateY(5px); transition: .25s; }
.chamber.revealed .target-readout { opacity: 1; transform: none; }
.demon-name { font: 650 15px/1.2 ui-monospace, monospace; letter-spacing: .11em; }
.demon-power, .entity-count { color: color-mix(in srgb, var(--glow) 85%, white); font: 9px/1.5 ui-monospace, monospace; letter-spacing: .13em; }

.hp-wrap { position: absolute; z-index: 7; right: 18px; top: 16px; width: 46px; height: 46px; opacity: 0; transition: .25s; color: var(--glow); }
.chamber.full-reveal .hp-wrap { opacity: 1; }
.hp-wrap svg { width: 100%; transform: rotate(-90deg); }
.hp-track, .hp-value { fill: none; stroke-width: 2; }
.hp-track { stroke: rgba(255,255,255,.14); }
.hp-value { stroke: currentColor; stroke-dasharray: 113.1; stroke-dashoffset: 0; transition: stroke-dashoffset .35s ease; }
.hp-wrap span { position: absolute; inset: 0; display: grid; place-items: center; font: 9px ui-monospace, monospace; }

.upper-timer { position: absolute; z-index: 7; left: 15px; top: 16px; color: #d9d5c9; font: 500 13px ui-monospace, monospace; letter-spacing: .12em; opacity: 0; transition: .2s; }
.chamber.revealed .upper-timer { opacity: 1; }
.pry-warning { position: absolute; z-index: 9; top: 14px; left: 50%; transform: translateX(-50%) translateY(-8px); color: #e3aca3; opacity: 0; display: flex; gap: 10px; font: 9px ui-monospace, monospace; letter-spacing: .09em; white-space: nowrap; transition: .2s; }
.pry-warning b { font-size: 11px; }
.chamber.prying .pry-warning { opacity: 1; transform: translateX(-50%); }

.door { position: absolute; z-index: 10; inset: 0; transform: translateX(calc(var(--door-x) * -1)); will-change: transform; }
.door-paper { position: absolute; inset: 0; background: linear-gradient(100deg, #d7d3c8 0, #eeece4 58%, #dad6ca 100%); border-right: 5px solid #bbb6aa; box-shadow: 8px 0 17px rgba(0,0,0,.55); }
.door-paper::before { content: ""; position: absolute; inset: 0; opacity: .18; background: repeating-linear-gradient(87deg, transparent 0 38px, rgba(75,70,58,.12) 39px, transparent 40px 75px); }
.door-edge { position: absolute; right: 0; top: 0; bottom: 0; width: 12px; background: linear-gradient(90deg, transparent, rgba(70,68,60,.17)); }
.door-stamp { position: absolute; left: 19px; top: 18px; border-left: 2px solid rgba(56,57,52,.35); padding-left: 8px; color: rgba(56,57,52,.52); font: 8px/1.4 ui-monospace, monospace; letter-spacing: .15em; }
.door-timer { position: absolute; inset: 38% auto auto 50%; transform: translate(-50%, -50%); display: grid; text-align: center; opacity: 0; transition: opacity .3s; }
.door-timer.visible { opacity: .8; }
.door-timer small { font: 7px/1.4 ui-monospace, monospace; letter-spacing: .2em; }
.door-timer strong { font: 400 22px/1.1 ui-monospace, monospace; letter-spacing: .11em; text-shadow: 0 1px 0 rgba(255,255,255,.6); }
.door-note { position: absolute; bottom: 18px; left: 18px; font: 8px ui-monospace, monospace; letter-spacing: .15em; color: rgba(55,56,52,.58); }
.grip { position: absolute; right: 28px; top: 50%; width: 27px; height: 68px; transform: translateY(-50%); border: 1px solid rgba(40,40,35,.2); border-radius: 12px; display: grid; align-content: center; gap: 5px; padding: 0 6px; box-shadow: inset 1px 1px 4px rgba(0,0,0,.08); }
.grip i { height: 1px; background: rgba(42,43,39,.35); }
.door-hitbox { position: absolute; z-index: 14; inset: 0 12px 0 12px; cursor: ew-resize; touch-action: none; }
.chamber.dragging .door { transition: none; }
.chamber.prying .door-paper { animation: tremble .15s steps(2,end) infinite; }
.chamber.forced::before { content: ""; position: absolute; z-index: 13; top: 0; bottom: 0; left: 0; width: 7px; background: #070807; box-shadow: 3px 0 10px #000; }
@keyframes tremble { from { transform: translateX(-1px); } to { transform: translateX(2px); } }

.instruction-line { padding: 0 14px; display: flex; align-items: center; gap: 9px; background: #dad6ca; border-top: 1px solid rgba(255,255,255,.4); }
.instruction-line .indicator { width: 6px; height: 6px; background: #64665f; border-radius: 50%; box-shadow: 0 0 0 3px rgba(100,102,95,.12); }
.instruction-line strong { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font: 9px ui-monospace, monospace; letter-spacing: .1em; }
.instruction-line.alert .indicator { background: var(--danger); box-shadow: 0 0 0 3px rgba(156,75,67,.15); }

.inscription { min-height: 0; padding: 12px 14px calc(10px + var(--safe-bottom)); display: grid; grid-template-rows: 30px minmax(165px, 1fr) 26px; background: linear-gradient(#ece9df, #e3dfd3); }
.sequence-row { display: flex; align-items: start; justify-content: center; position: relative; }
.sequence { min-height: 25px; display: flex; gap: 5px; justify-content: center; align-items: center; }
.sequence .empty { color: var(--quiet); font: 8px ui-monospace, monospace; letter-spacing: .15em; }
.sequence .combo-label { margin-right: 2px; color: var(--quiet); font: 7px ui-monospace, monospace; letter-spacing: .11em; white-space: nowrap; }
.sequence .combo-label b { display: block; color: var(--danger); font-size: 8px; font-weight: 600; }
.sequence .chip { min-width: 27px; height: 24px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 3px; background: rgba(255,255,255,.35); font: 11px ui-monospace, monospace; transition: background .18s, border-color .18s, opacity .18s; }
.sequence .chip img { width: 19px; height: 19px; object-fit: contain; filter: grayscale(1) contrast(3); opacity: .62; }
.sequence .chip.done { background: rgba(154,115,56,.2); border-color: rgba(130,91,35,.45); }
.sequence .chip.done img { opacity: .35; }
.sequence .chip.next { border-color: var(--danger); box-shadow: 0 0 0 1px rgba(156,75,67,.12); }
.sequence .chip.next img { opacity: .95; }
.sequence .slot { color: #99988f; border-style: dashed; }
.cancel-sequence { position: absolute; right: 0; top: -4px; border: 0; background: none; font-size: 21px; color: var(--quiet); cursor: pointer; }
.draw-surface { position: relative; min-height: 0; border: 1px solid rgba(54,55,50,.22); border-radius: 6px; overflow: hidden; touch-action: none; background: rgba(255,255,255,.2); box-shadow: inset 0 1px 8px rgba(42,42,35,.035); }
.draw-surface::before, .draw-surface::after { content: ""; position: absolute; background: rgba(50,50,45,.055); pointer-events: none; }
.draw-surface::before { width: 1px; height: 100%; left: 50%; }
.draw-surface::after { height: 1px; width: 100%; top: 50%; }
#inkCanvas { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 3; }
.trace-guide { position: absolute; inset: 13%; display: grid; place-items: center; opacity: .075; transition: opacity .25s; pointer-events: none; }
.trace-guide.visible { opacity: .24; }
.trace-guide img { width: 52%; height: 72%; object-fit: contain; filter: grayscale(1) contrast(4); }
.trace-guide span { position: absolute; bottom: -6%; font: 8px ui-monospace, monospace; letter-spacing: .2em; }
.recognition-flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; border: 1px solid transparent; }
.recognition-flash.success { animation: flash-ok .5s ease; }
.recognition-flash.fail { animation: flash-fail .42s ease; }
@keyframes flash-ok { 30% { opacity: 1; border-color: rgba(174,119,40,.6); box-shadow: inset 0 0 28px rgba(211,155,67,.14); } }
@keyframes flash-fail { 30% { opacity: 1; border-color: rgba(135,76,70,.5); } }
.draw-footer { display: flex; align-items: end; justify-content: space-between; color: var(--quiet); font: 8px ui-monospace, monospace; letter-spacing: .1em; }
.draw-footer button { border: 0; border-bottom: 1px solid currentColor; padding: 2px 0; background: none; color: inherit; font: inherit; cursor: pointer; }

.scrim { position: fixed; inset: 0; z-index: 49; background: rgba(10,11,10,.5); backdrop-filter: blur(2px); }
.side-menu { position: fixed; z-index: 50; top: 0; bottom: 0; left: max(0px, calc(50% - 260px)); width: min(90vw, 430px); padding: calc(18px + var(--safe-top)) 18px calc(15px + var(--safe-bottom)); display: grid; grid-template-rows: auto auto 1fr auto; background: #ece9df; box-shadow: 12px 0 35px rgba(0,0,0,.35); transform: translateX(-110%); transition: transform .28s cubic-bezier(.2,.8,.2,1); }
.side-menu.open { transform: none; }
.menu-header { display: flex; justify-content: space-between; align-items: start; padding-bottom: 14px; }
.menu-header small { color: var(--quiet); font: 8px ui-monospace, monospace; letter-spacing: .15em; }
.menu-header h2 { margin: 3px 0 0; font: 500 24px/1.1 Georgia, serif; }
.menu-tabs { display: flex; overflow-x: auto; border-block: 1px solid var(--line); }
.menu-tabs button { padding: 11px 12px; border: 0; border-bottom: 2px solid transparent; background: none; color: var(--quiet); font: 9px ui-monospace, monospace; text-transform: uppercase; cursor: pointer; white-space: nowrap; }
.menu-tabs button.active { color: var(--ink); border-color: var(--danger); }
.menu-tabs em { min-width: 15px; height: 15px; display: inline-grid; place-items: center; border-radius: 50%; background: #d5d0c4; font-style: normal; }
.menu-content { overflow-y: auto; padding: 17px 2px 20px; }
.menu-footer { border-top: 1px solid var(--line); padding-top: 12px; display: flex; justify-content: space-between; align-items: center; color: var(--quiet); font: 7px ui-monospace, monospace; letter-spacing: .1em; }
.menu-footer button { border: 0; background: none; color: #8e4e48; font: inherit; cursor: pointer; }
.panel-intro { margin: 0 0 14px; color: var(--quiet); font-size: 12px; line-height: 1.5; }
.record-list { display: grid; gap: 10px; }
.practice-card { margin-bottom: 14px; padding: 12px; border: 1px solid var(--line); border-radius: 5px; background: rgba(255,255,255,.28); }
.practice-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 9px; }
.practice-heading small { color: var(--danger); font: 7px ui-monospace, monospace; letter-spacing: .14em; }
.practice-heading h3 { margin: 3px 0 0; font: 650 11px ui-monospace, monospace; letter-spacing: .1em; }
.practice-heading > img { width: 42px; height: 42px; padding: 4px; object-fit: contain; border: 1px solid var(--line); filter: grayscale(1) contrast(3); }
.practice-picks { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; margin-bottom: 9px; }
.practice-picks button { min-width: 0; padding: 5px 2px 4px; border: 1px solid var(--line); border-radius: 3px; background: rgba(255,255,255,.22); cursor: pointer; }
.practice-picks button.active { border-color: var(--danger); background: rgba(156,75,67,.08); }
.practice-picks img { display: block; width: 22px; height: 22px; margin: auto; object-fit: contain; filter: grayscale(1) contrast(3); }
.practice-picks span { display: block; overflow: hidden; margin-top: 2px; color: var(--quiet); font: 6px ui-monospace, monospace; text-overflow: ellipsis; text-transform: uppercase; }
.practice-surface { position: relative; height: 150px; overflow: hidden; border: 1px solid var(--line); border-radius: 4px; background: rgba(255,255,255,.2); touch-action: none; }
.practice-surface::before, .practice-surface::after { content: ""; position: absolute; background: rgba(50,50,45,.055); pointer-events: none; }
.practice-surface::before { left: 50%; width: 1px; height: 100%; }
.practice-surface::after { top: 50%; width: 100%; height: 1px; }
.practice-surface canvas { position: absolute; inset: 0; z-index: 2; width: 100%; height: 100%; }
.practice-surface > span { position: absolute; inset: auto 0 7px; color: #99988f; text-align: center; font: 7px ui-monospace, monospace; letter-spacing: .14em; }
.practice-result { min-height: 29px; margin: 8px 0 0; color: var(--quiet); font: 8px/1.45 ui-monospace, monospace; letter-spacing: .06em; }
.practice-result[data-state="good"] { color: #657842; }
.practice-result[data-state="bad"] { color: var(--danger); }
.spell-card, .record-card, .upgrade-card { border: 1px solid var(--line); border-radius: 5px; background: rgba(255,255,255,.25); padding: 12px; }
.spell-card { display: grid; grid-template-columns: 55px 1fr; gap: 12px; }
.spell-card.locked { opacity: .5; }
.spell-card img { width: 52px; height: 52px; padding: 6px; object-fit: contain; border: 1px solid var(--line); filter: grayscale(1) contrast(3); }
.spell-card h3, .record-card h3, .upgrade-card h3 { margin: 0 0 4px; font: 650 11px ui-monospace, monospace; letter-spacing: .1em; }
.spell-card p, .record-card p, .upgrade-card p { margin: 0; color: #61625c; font-size: 11px; line-height: 1.45; }
.tagline { margin-top: 7px; display: flex; gap: 5px; flex-wrap: wrap; }
.tagline span { padding: 3px 5px; background: #d8d3c7; border-radius: 2px; font: 7px ui-monospace, monospace; letter-spacing: .08em; }
.record-card { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.record-card .meta { color: var(--quiet); font: 8px ui-monospace, monospace; letter-spacing: .08em; }
.record-card button, .upgrade-card button, .briefing button { padding: 9px 12px; border: 1px solid #55564f; border-radius: 3px; background: #30312d; color: #efede6; font: 9px ui-monospace, monospace; letter-spacing: .1em; cursor: pointer; }
.record-card button:disabled, .upgrade-card button:disabled { opacity: .35; cursor: not-allowed; }
.empty-record { margin: 35px auto; max-width: 230px; color: var(--quiet); text-align: center; font: 10px/1.7 ui-monospace, monospace; letter-spacing: .08em; }
.upgrade-card strong { display: block; margin: 18px 0 3px; font: 400 34px ui-monospace, monospace; }
.upgrade-card button { width: 100%; margin-top: 14px; }

.briefing { position: fixed; z-index: 70; inset: 0; display: grid; place-items: center; padding: 20px; background: rgba(13,14,13,.67); backdrop-filter: blur(3px); }
.briefing[hidden] { display: none; }
.briefing-card { width: min(100%, 350px); padding: 24px; background: #e9e5da; border: 1px solid #aaa499; box-shadow: 0 18px 60px rgba(0,0,0,.4); }
.briefing-card small { color: var(--danger); font: 8px ui-monospace, monospace; letter-spacing: .16em; }
.briefing-card h2 { margin: 9px 0; font: 500 24px Georgia, serif; }
.briefing-card p { margin: 0 0 20px; color: #565751; font-size: 13px; line-height: 1.55; }

.toast { position: fixed; z-index: 80; left: 50%; bottom: calc(24px + var(--safe-bottom)); max-width: min(90vw, 420px); padding: 9px 14px; border-radius: 3px; background: #2a2b28; color: #f0eee7; font: 9px ui-monospace, monospace; letter-spacing: .1em; text-align: center; opacity: 0; transform: translate(-50%, 12px); pointer-events: none; transition: .22s; }
.toast.visible { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: #713d39; }

@media (max-height: 650px) {
  .app { grid-template-rows: auto minmax(220px, 44dvh) minmax(220px, 1fr); }
  .topbar { min-height: calc(52px + var(--safe-top)); padding-block: calc(6px + var(--safe-top)) 6px; }
  .inscription { padding-top: 7px; grid-template-rows: 26px minmax(140px,1fr) 22px; }
  .grip { height: 55px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: .001ms !important; }
}
