/* Fray & the Spiral Isle. Phone first: everything fits 360 px wide. */
:root {
    --bg: #1b2a2e;
    --panel: rgba(24, 34, 36, 0.94);
    --paper: #f3ead6;
    --ink: #2b2a22;
    --line: rgba(255, 255, 255, 0.2);
    --text: #f4f1e6;
    --dim: #b9c2b4;
    --accent: #f0c96a;
    --serif: 'Cormorant Garamond', 'Iowan Old Style', Palatino, Georgia, serif;
    --safe-t: env(safe-area-inset-top, 0px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-l: env(safe-area-inset-left, 0px);
    --safe-r: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
    margin: 0; padding: 0; height: 100%;
    background: var(--bg); color: var(--text);
    font-family: Nunito, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden; overscroll-behavior: none;
    user-select: none; -webkit-user-select: none;
}
[hidden] { display: none !important; }
.dim { color: var(--dim); font-size: 13px; }
.grow { flex: 1; }

/* The view sizes the canvas in px from JS. Never 100vw/100vh: on phones 100vh is taller than the visible area. */
#view { position: fixed; left: 0; top: 0; display: block; touch-action: none; }
#fade { position: fixed; inset: 0; z-index: 5; background: #f6f1e2; opacity: 0; pointer-events: none; transition: opacity 0.5s ease; }
#fade.on { opacity: 1; transition: opacity 0.25s ease; }

#back-link {
    position: fixed; top: calc(8px + var(--safe-t)); left: calc(8px + var(--safe-l)); z-index: 99999;
    font: 13px Arial, Helvetica, sans-serif; color: #fff; text-decoration: none;
    background: rgba(0, 0, 0, 0.55); padding: 6px 12px; border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.25);
}
#back-link:hover { background: rgba(0, 0, 0, 0.8); }

/* ---------------------------------------------------------------- buttons */
.btn, .ibtn, .tbtn {
    font: inherit; color: var(--text); cursor: pointer;
    min-height: 44px; min-width: 44px;           /* finger-sized */
    border: 1px solid var(--line); border-radius: 10px;
    background: rgba(255, 255, 255, 0.08);
}
.btn { padding: 10px 18px; font-size: 16px; }
.btn.primary { background: var(--accent); color: #2a2008; border-color: transparent; font-weight: 700; }
.btn.danger { border-color: #e08a7a; color: #f3c3b8; }
.btn:focus-visible, .ibtn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ibtn { font-size: 18px; background: rgba(10, 16, 18, 0.55); backdrop-filter: blur(4px); }

/* ---------------------------------------------------------------- HUD */
#hud { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
#hud-top {
    position: absolute; top: calc(8px + var(--safe-t)); right: calc(8px + var(--safe-r)); left: calc(104px + var(--safe-l));
    display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end;
}
#hud-top .ibtn { pointer-events: auto; }
#pendant { display: flex; gap: 4px; padding: 7px 10px; border-radius: 999px; background: rgba(10, 16, 18, 0.55); border: 1px solid var(--line); }
#pendant i { width: 13px; height: 13px; border-radius: 50%; border: 1.5px solid rgba(255, 255, 255, 0.55); display: block; }
#pendant i.on { border-color: #fff; box-shadow: 0 0 8px currentColor; background: currentColor; }
#hud-items { display: flex; gap: 6px; flex-wrap: wrap; }
#hud-items span { padding: 5px 9px; border-radius: 999px; font-size: 13px; background: rgba(10, 16, 18, 0.55); border: 1px solid var(--line); white-space: nowrap; }
#place {
    position: absolute; left: 0; right: 0; top: 22%; text-align: center;
    font: italic 500 clamp(26px, 6vw, 44px) var(--serif); letter-spacing: 0.04em;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7); opacity: 0; transition: opacity 1.2s ease;
}
#place.on { opacity: 1; }
#reticle { position: absolute; left: 50%; top: 50%; width: 6px; height: 6px; margin: -3px; border-radius: 50%; background: rgba(255, 255, 255, 0.7); box-shadow: 0 0 4px rgba(0, 0, 0, 0.6); }
#prompt {
    position: absolute; left: 50%; top: 58%; transform: translateX(-50%); max-width: 92vw;
    display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 999px;
    background: rgba(10, 16, 18, 0.7); border: 1px solid var(--line); font-size: 15px; white-space: nowrap;
}
kbd { font: 700 13px Nunito, system-ui, sans-serif; padding: 2px 8px; border-radius: 6px; background: var(--accent); color: #2a2008; }
#subtitle {
    position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(26px + var(--safe-b)); width: min(620px, 94vw);
    display: flex; gap: 10px; align-items: center; padding: 10px 14px; border-radius: 14px;
    background: rgba(10, 16, 18, 0.78); border: 1px solid var(--line); font-size: 16px; line-height: 1.35;
}
#subtitle img, #tunnel-head img { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; flex: none; background: #5d7a45; border: 2px solid var(--accent); }
#subtitle b, #tunnel-head b { display: block; color: var(--accent); font-size: 13px; letter-spacing: 0.05em; }
body.touching #subtitle { bottom: calc(150px + var(--safe-b)); }

/* ---------------------------------------------------------------- touch deck */
#touch { position: fixed; inset: 0; z-index: 8; pointer-events: none; }
#stick-zone { position: absolute; left: 0; bottom: 0; width: 46%; height: 62%; pointer-events: auto; touch-action: none; }
#look-zone { position: absolute; right: 0; bottom: 0; width: 54%; height: 78%; pointer-events: auto; touch-action: none; }
#stick { position: absolute; width: 0; height: 0; opacity: 0.35; left: 90px; bottom: 120px; }
#stick.on { opacity: 0.9; }
#stick .ring { position: absolute; width: 120px; height: 120px; left: -60px; top: -60px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.5); }
#stick .knob { position: absolute; width: 54px; height: 54px; left: -27px; top: -27px; border-radius: 50%; background: rgba(255, 255, 255, 0.55); }
.tbtn {
    position: absolute; pointer-events: auto; touch-action: none;
    right: calc(22px + var(--safe-r)); bottom: calc(44px + var(--safe-b));
    width: 78px; height: 78px; border-radius: 50%; font-size: 30px;
    background: rgba(240, 201, 106, 0.32);
}
.tbtn.ready { background: rgba(240, 201, 106, 0.85); box-shadow: 0 0 18px rgba(240, 201, 106, 0.7); }

/* ---------------------------------------------------------------- cards, inscriptions, notes */
#card {
    position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
    padding: calc(52px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
    background: rgba(8, 12, 14, 0.5);
}
#card-box {
    width: min(760px, 100%); max-height: 100%; overflow-y: auto; touch-action: pan-y;
    background: var(--paper); color: var(--ink); border-radius: 14px; box-shadow: 0 12px 50px rgba(0, 0, 0, 0.55);
    scrollbar-width: thin;
}
#card-img { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 14px 14px 0 0; background: #c9d6cf; }
#card-body { padding: 16px 22px 18px; }
#card-kicker { font-size: 12px; letter-spacing: 0.14em; text-transform: uppercase; color: #8a7a4a; }
#card-head { margin: 2px 0 8px; font: 700 clamp(24px, 5vw, 34px) var(--serif); }
#card-text { margin: 0; font: 500 clamp(17px, 2.6vw, 21px)/1.5 var(--serif); }
#card.lore #card-box { background: #dcdfd6; width: min(560px, 100%); border: 6px double #8f968a; }
#card.note #card-box { width: min(480px, 100%); }
#card .btn.primary { background: #3d5a4a; color: #fff; }

/* ---------------------------------------------------------------- the tunnel panel */
#tunnel { position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; display: flex; justify-content: center; padding: 0 calc(8px + var(--safe-r)) calc(8px + var(--safe-b)) calc(8px + var(--safe-l)); pointer-events: none; }
#tunnel-box { width: min(560px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px; pointer-events: auto; }
#tunnel-head { display: flex; gap: 10px; align-items: center; min-height: 64px; }
#tunnel-say { display: block; font-size: 15px; line-height: 1.3; }
#tunnel-mid { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 8px; }
#echo { width: min(170px, 38vw); height: min(170px, 38vw); border-radius: 10px; background: #0b0e12; border: 1px solid var(--line); }
#dpad { display: grid; grid-template-columns: 56px minmax(96px, auto) 56px; grid-template-rows: repeat(3, 48px); gap: 6px; }
#dpad [data-d="n"] { grid-column: 2; grid-row: 1; }
#dpad [data-d="w"] { grid-column: 1; grid-row: 2; }
#tun-feel { grid-column: 2; grid-row: 2; padding: 6px 8px; font-size: 14px; }
#dpad [data-d="e"] { grid-column: 3; grid-row: 2; }
#dpad [data-d="s"] { grid-column: 2; grid-row: 3; }
#dpad .btn { padding: 0; }
#tunnel .sheet-foot { justify-content: space-between; align-items: center; margin-top: 8px; }

/* ---------------------------------------------------------------- screens */
.screen {
    position: fixed; inset: 0; z-index: 50;
    display: flex; align-items: center; justify-content: center;
    /* top padding clears the fixed back link, so a scrolled button never sits under it */
    padding: calc(52px + var(--safe-t)) calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) calc(12px + var(--safe-l));
    background: rgba(8, 12, 14, 0.55); overflow-y: auto;
}
.sheet {
    width: min(460px, 100%); max-height: 100%; overflow-y: auto; touch-action: pan-y;
    background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px;
    scrollbar-width: thin; scrollbar-color: rgba(255, 255, 255, 0.3) transparent; --sheet-pt: 18px; --sheet-px: 18px; --sheet-pb: 18px;
}
.sheet.wide { width: min(720px, 100%); }
.sheet h1, .sheet h2 { margin: 0 0 12px; font-family: var(--serif); }
.sheet p { line-height: 1.5; margin: 0 0 10px; }
#title { background: linear-gradient(rgba(8, 12, 14, 0.15), rgba(8, 12, 14, 0.6)), url(art/card-intro1.webp) center / cover no-repeat, #27424a; }
.title-sheet { background: rgba(18, 28, 30, 0.82); backdrop-filter: blur(6px); }
.logo { text-align: center; color: var(--accent); line-height: 0.95; }
.logo span { display: block; font-size: clamp(46px, 13vw, 76px); letter-spacing: 0.02em; }
.logo small { display: block; font-size: clamp(20px, 5.5vw, 30px); font-style: italic; font-weight: 500; color: var(--text); }
.tag { text-align: center; color: var(--dim); font-style: italic; }
.menu { display: flex; flex-direction: column; gap: 10px; }
.sheet-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 14px; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 44px; }
.row input[type=checkbox] { width: 24px; height: 24px; }
.version { text-align: center; color: var(--dim); font-size: 12px; margin-top: 12px; }
table { border-collapse: collapse; width: 100%; margin-bottom: 10px; }
td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
td:first-child { color: var(--accent); white-space: nowrap; }

/* ---------------------------------------------------------------- journal */
.tabs { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.tab.on { background: var(--accent); color: #2a2008; border-color: transparent; font-weight: 700; }
#journal-body { min-height: 180px; }
.gem-row { display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.gem-row img, .gem-row .dot { width: 46px; height: 46px; flex: none; border-radius: 50%; object-fit: contain; }
.gem-row .dot { border: 2px dashed rgba(255, 255, 255, 0.35); }
.gem-row b { font: 700 19px var(--serif); display: block; }
.gem-row.off { opacity: 0.55; }
.story { padding: 8px 0; border-bottom: 1px solid var(--line); }
.story b { font: 700 18px var(--serif); display: block; color: var(--accent); }
.story span { font: 500 17px/1.45 var(--serif); }
.story.unread span { color: var(--dim); font-style: italic; }
.zone-head { margin: 14px 0 2px; font: italic 500 22px var(--serif); }
.keep { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.keep img { width: 36px; height: 36px; object-fit: contain; }
.keep .bar { flex: 1; height: 8px; border-radius: 4px; background: rgba(255, 255, 255, 0.14); overflow: hidden; }
.keep .bar i { display: block; height: 100%; background: var(--accent); }
.keep.done b { color: var(--accent); }

#toasts { position: fixed; left: 50%; top: calc(64px + var(--safe-t)); transform: translateX(-50%); z-index: 20; pointer-events: none; display: flex; flex-direction: column; gap: 6px; align-items: center; width: 94vw; }
.toast { background: rgba(10, 16, 18, 0.82); border: 1px solid var(--line); border-radius: 999px; padding: 7px 16px; animation: toast 3s forwards; display: flex; align-items: center; gap: 8px; max-width: 100%; }
.toast img { width: 24px; height: 24px; object-fit: contain; }
@keyframes toast { 0% { opacity: 0; transform: translateY(-6px); } 8%, 82% { opacity: 1; transform: none; } 100% { opacity: 0; } }
.noscript { position: fixed; inset: 0; display: grid; place-items: center; }

@media (max-width: 520px) {
    #hud-top { left: calc(8px + var(--safe-l)); top: calc(48px + var(--safe-t)); }
    #toasts { top: calc(104px + var(--safe-t)); }
    #tunnel-keys { display: none; }
}
@media (max-height: 460px) {
    #card-box { display: flex; }
    #card-img { width: 42%; aspect-ratio: auto; border-radius: 14px 0 0 14px; }
    #card-body { flex: 1; overflow-y: auto; padding: 10px 16px 0; }
    #card-text { font-size: 16px; line-height: 1.4; }
    #card .sheet-foot { position: sticky; bottom: 0; margin-top: 6px; padding: 6px 0 10px; background: linear-gradient(transparent, var(--paper) 35%); }
    #tunnel-head { min-height: 0; }
    #echo { width: 120px; height: 120px; }
    #dpad { grid-template-rows: repeat(3, 44px); }
    #place { top: 14%; }
}
@media (prefers-reduced-motion: reduce) { #fade, #place { transition: none; } .toast { animation-duration: 3s; } }

/* A long sheet scrolls, but its foot (Done, Close, Continue) and its tab bar don't: they are pinned to the
   sheet's bottom and top edges over an opaque strip of the sheet's own colour, so nothing shows through.
   A foot inside a wrapper is pinned too, without the bleed. --sheet-p* are set wherever a .sheet's padding is. */
.sheet > .sheet-foot { position: sticky; bottom: calc(-1 * var(--sheet-pb)); z-index: 5; margin-inline: calc(-1 * var(--sheet-px)); margin-bottom: calc(-1 * var(--sheet-pb)); padding: 10px var(--sheet-px) var(--sheet-pb); background: #172022; border-top: 1px solid rgba(255, 255, 255, 0.2); }
.sheet * > .sheet-foot { position: sticky; bottom: calc(-1 * var(--sheet-pb)); z-index: 5; padding-bottom: var(--sheet-pb); background: #172022; }
.sheet > .tabs { position: sticky; top: calc(-1 * var(--sheet-pt)); z-index: 5; margin-inline: calc(-1 * var(--sheet-px)); padding: 8px var(--sheet-px); background: #172022; }

