:root {
  --ink: #f3e9d6;
  --muted: #b9a888;
  --gold: #d9a44a;
  --gold-2: #f0c873;
  --blood: #b3261e;
  --steel: #9aa3ad;
  --panel: rgba(20, 15, 10, 0.78);
  --line: rgba(217, 164, 74, 0.35);
  --good: #9fd17a;
  --warn: #f0b550;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #120e0a; color: var(--ink);
  font-family: 'EB Garamond', Georgia, serif; font-size: 17px; overscroll-behavior: none; }
body { touch-action: none; user-select: none; -webkit-user-select: none; }
[hidden] { display: none !important; }
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
#vignette { position: fixed; inset: 0; pointer-events: none; background: radial-gradient(ellipse at center, transparent 55%, rgba(0,0,0,0.55) 100%); }
#hurtFlash { position: fixed; inset: 0; pointer-events: none; background: radial-gradient(ellipse at center, transparent 30%, rgba(160,0,0,0.65)); opacity: 0; transition: opacity 0.35s; }
body.hurt #hurtFlash { opacity: 1; transition: none; }

/* menus */
#ui { position: fixed; inset: 0; pointer-events: none; }
.screen { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top)); padding-bottom: max(16px, env(safe-area-inset-bottom)); overflow-y: auto; pointer-events: auto; touch-action: pan-y; }
.screen.side { justify-content: flex-end; }
@media (max-width: 760px) { .screen.side { justify-content: center; align-items: flex-end; } .screen.side .card { max-height: 58vh; overflow-y: auto; } }
.card { width: min(440px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); box-shadow: 0 20px 60px rgba(0,0,0,0.5); user-select: text; }
.card.center { text-align: center; }
.title { font-family: Cinzel, serif; font-weight: 900; font-size: 38px; letter-spacing: 2px; margin: 0 0 6px; text-align: center;
  background: linear-gradient(180deg, #fff3d6, #d9a44a 60%, #8a5a1a); -webkit-background-clip: text; background-clip: text; color: transparent; }
.title span { font-weight: 500; }
h2 { font-family: Cinzel, serif; margin: 0 0 10px; color: var(--gold-2); letter-spacing: 1px; }
.tag, .hint { color: var(--muted); margin: 6px 0 12px; }
.who { text-align: center; color: var(--muted); margin: 0 0 16px; }
.who b { color: var(--ink); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #777; margin-left: 4px; }
.dot.on { background: #6fcf5a; box-shadow: 0 0 8px #6fcf5a; }
label { display: block; margin: 10px 0; color: var(--muted); font-size: 15px; }
label small { color: #8f8068; }
input, select { width: 100%; margin-top: 4px; padding: 12px; border-radius: 9px; border: 1px solid var(--line); background: rgba(0,0,0,0.45);
  color: var(--ink); font: inherit; font-size: 16px; }
select { width: auto; padding: 6px 8px; margin: 0 0 0 6px; }
input:focus { outline: 2px solid var(--gold); }
button { font-family: Cinzel, serif; font-weight: 700; font-size: 16px; letter-spacing: 0.5px; color: #1a1108; cursor: pointer;
  background: linear-gradient(180deg, #f0c873, #c48a32); border: 1px solid #f7d993; border-radius: 10px; padding: 13px 16px; width: 100%; margin-top: 10px; }
button:active { transform: translateY(1px); filter: brightness(0.95); }
button.ghost { background: rgba(0,0,0,0.3); color: var(--ink); border-color: var(--line); }
button.link { background: none; border: none; color: var(--muted); font-family: inherit; font-weight: 400; text-decoration: underline; }
button.small { width: auto; padding: 7px 12px; font-size: 13px; margin: 0; }
.row { display: flex; gap: 10px; }
.row button { flex: 1; }
.tabs { display: flex; gap: 6px; margin: 10px 0; }
.tab { margin: 0; background: rgba(0,0,0,0.3); color: var(--muted); border-color: var(--line); padding: 9px; }
.tab.on { background: rgba(217,164,74,0.2); color: var(--gold-2); }
.error { color: #ff8a7a; min-height: 1.2em; margin: 8px 0 0; }
.ok { color: var(--good); min-height: 1.2em; margin: 8px 0 0; }
.settings { display: flex; flex-wrap: wrap; gap: 4px 16px; justify-content: center; margin-top: 14px; }
.settings label { display: flex; align-items: center; gap: 6px; margin: 4px 0; }
.settings input { width: auto; margin: 0; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice { margin: 0; text-align: left; background: rgba(0,0,0,0.35); color: var(--ink); border-color: var(--line); font-family: inherit; padding: 10px 12px; }
.choice b { display: block; font-family: Cinzel, serif; color: var(--gold-2); font-size: 15px; }
.choice small { color: var(--muted); font-weight: 400; font-size: 13px; }
.choice.on { background: rgba(217,164,74,0.22); border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold); }
.search { display: flex; gap: 8px; margin-bottom: 10px; }
.search input { margin: 0; }
.search button { width: auto; margin: 0; }
.players { list-style: none; padding: 0; margin: 12px 0 0; max-height: 36vh; overflow-y: auto; }
.players li { display: flex; align-items: center; gap: 10px; padding: 10px; border-bottom: 1px solid rgba(217,164,74,0.15); }
.players li .pname { flex: 1; }
.players li .pname small { display: block; color: var(--muted); }
.players li button { width: auto; margin: 0; padding: 8px 12px; font-size: 14px; }
.players .empty { color: var(--muted); justify-content: center; }
.how ol { padding-left: 20px; line-height: 1.45; }
.how li { margin-bottom: 8px; }
.opt-group { margin: 10px 0 14px; }
.opt-group > div:first-child { color: var(--muted); font-size: 14px; margin-bottom: 6px; text-transform: uppercase; letter-spacing: 1px; }
.opts { display: flex; flex-wrap: wrap; gap: 6px; }
.opts button { width: auto; margin: 0; padding: 7px 11px; font-size: 13px; background: rgba(0,0,0,0.35); color: var(--ink); border-color: var(--line); }
.opts button.on { background: rgba(217,164,74,0.25); border-color: var(--gold); color: var(--gold-2); }
.opts button.swatch { width: 34px; height: 34px; padding: 0; border-radius: 50%; border-width: 2px; }
.opts button.swatch.on { box-shadow: 0 0 0 2px var(--gold-2); }
.opts button.swatch.none { background: repeating-linear-gradient(45deg, #333 0 4px, #666 4px 8px); }

.overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0,0,0,0.45); z-index: 30; }
#endTitle { font-family: Cinzel, serif; font-size: 52px; margin: 0; }
#endTitle.win { color: var(--gold-2); text-shadow: 0 0 30px rgba(240,200,115,0.6); }
#endTitle.lose { color: #d9574b; }
#toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--panel); border: 1px solid var(--line);
  padding: 10px 16px; border-radius: 10px; z-index: 40; max-width: calc(100% - 32px); text-align: center; }

/* in duel */
#touch { position: fixed; inset: 0; display: none; touch-action: none; }
body.in-duel #touch { display: block; }
#swipeTrails { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#hud { position: fixed; inset: 0; pointer-events: none; padding: max(10px, env(safe-area-inset-top)) 16px 16px; }
.bars { display: flex; align-items: flex-start; gap: 12px; }
.bars .side { flex: 1; }
.bars .foe { text-align: right; }
.bars .name { font-family: Cinzel, serif; font-weight: 700; font-size: 15px; text-shadow: 0 1px 3px #000; margin-bottom: 4px; }
.vs { font-size: 20px; color: var(--gold); padding-top: 14px; }
.hp { position: relative; height: 16px; border-radius: 4px; background: rgba(0,0,0,0.6); border: 1px solid rgba(240,200,115,0.5); overflow: hidden; }
.hp .fill { position: absolute; inset: 0 auto 0 0; width: 100%; background: linear-gradient(180deg, #e0574a, #8e1b14); transition: width 0.4s ease; }
.foe .hp .fill { left: auto; right: 0; }
.hp .num { position: absolute; inset: 0; font-size: 12px; text-align: center; line-height: 16px; font-weight: 600; text-shadow: 0 1px 2px #000; }
#banner { position: absolute; top: 22%; left: 0; right: 0; text-align: center; text-shadow: 0 2px 10px #000, 0 0 2px #000; }
#banner .big { font-family: Cinzel, serif; font-weight: 900; font-size: clamp(26px, 6vw, 44px); letter-spacing: 2px; animation: pop 0.35s ease; }
#banner .sub { font-size: 17px; color: var(--ink); opacity: 0.9; margin-top: 2px; padding: 0 16px; }
#banner.attack .big { color: var(--gold-2); }
#banner.defend .big { color: #a7c6e8; }
@keyframes pop { from { transform: scale(1.3); opacity: 0; } to { transform: scale(1); opacity: 1; } }
#timer { position: absolute; top: 64px; left: 50%; transform: translateX(-50%); width: 40px; height: 40px; border-radius: 50%; border: 2px solid var(--gold);
  display: grid; place-items: center; font-family: Cinzel, serif; font-weight: 700; background: rgba(0,0,0,0.5); }
#popups { position: absolute; top: 40%; left: 0; right: 0; text-align: center; }
.popup { font-family: Cinzel, serif; font-weight: 900; font-size: clamp(24px, 5vw, 38px); text-shadow: 0 2px 12px #000; animation: rise 1.4s ease forwards; }
.popup.good { color: var(--gold-2); }
.popup.bad { color: #ff6b5c; }
.popup.warn { color: var(--warn); font-size: 22px; }
@keyframes rise { 0% { transform: translateY(10px) scale(1.2); opacity: 0; } 15% { opacity: 1; transform: none; } 75% { opacity: 1; } 100% { transform: translateY(-40px); opacity: 0; } }
#guide { position: absolute; left: 50%; top: 46%; width: 0; height: 0; }
#guide .streak { position: absolute; left: -32vmin; top: -3px; width: 64vmin; height: 6px; border-radius: 3px; transform: rotate(var(--ang));
  background: linear-gradient(90deg, rgba(255,90,60,0), rgba(255,120,80,0.9) 80%, #fff); box-shadow: 0 0 14px rgba(255,90,60,0.8); opacity: 0; }
#guide.run .streak { animation: streak var(--ms) linear forwards; }
@keyframes streak { 0% { opacity: 0; clip-path: inset(0 100% 0 0); } 40% { opacity: 0.9; } 100% { opacity: 0.9; clip-path: inset(0 0 0 0); } }
#btnYield { position: absolute; right: 16px; top: max(56px, calc(env(safe-area-inset-top) + 46px)); pointer-events: auto; }
#swordPad { position: absolute; right: max(24px, env(safe-area-inset-right)); bottom: max(24px, env(safe-area-inset-bottom)); width: 120px; height: 120px; border-radius: 50%;
  border: 2px dashed rgba(240,200,115,0.35); display: none; place-items: center; text-align: center; color: rgba(243,233,214,0.55); font-family: Cinzel, serif; font-size: 13px; }
body.shield-mode #swordPad { display: grid; }
#shieldStick { position: fixed; left: max(28px, env(safe-area-inset-left)); bottom: max(28px, env(safe-area-inset-bottom)); width: 140px; height: 140px; pointer-events: none; z-index: 5; }
#shieldStick .ring { position: absolute; inset: 0; border-radius: 50%; background: radial-gradient(circle, rgba(0,0,0,0.15), rgba(0,0,0,0.45)); border: 2px solid rgba(240,200,115,0.45); }
#shieldStick .knob { position: absolute; left: 50%; top: 50%; width: 62px; height: 62px; margin: -31px 0 0 -31px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #f0d9a0, #9b6c2a 70%); border: 2px solid #f7d993; box-shadow: 0 4px 12px rgba(0,0,0,0.5); }
#shieldStick .label { position: absolute; top: -22px; width: 100%; text-align: center; font-family: Cinzel, serif; font-size: 13px; color: rgba(243,233,214,0.7); }

@media (max-height: 500px) {
  #banner { top: 17%; }
  #banner .big { font-size: 26px; }
  #banner .sub { font-size: 14px; }
  #popups { top: 34%; }
  #timer { top: 52px; width: 34px; height: 34px; }
  #shieldStick { width: 120px; height: 120px; }
}

/* ---------- stage: drawn sideways when the page is locked in portrait (tilt guard) ---------- */
#app { position: fixed; inset: 0; overflow: hidden; }
body.rot90 #app, body.rot270 #app { inset: auto; top: 0; left: 0; width: 100vh; height: 100vw; transform-origin: top left; }
body.rot90 #app { transform: translate(100vw, 0) rotate(90deg); }
body.rot270 #app { transform: translate(0, 100vh) rotate(-90deg); }

/* ---------- Free Fight ---------- */
#freeLayer { position: fixed; inset: 0; display: none; touch-action: none; }
body.in-free #freeLayer { display: block; }
#freeTrails { position: fixed; left: 0; top: 0; pointer-events: none; }
.stick { position: fixed; width: 124px; height: 124px; pointer-events: none; z-index: 5; }
.stick .ring { position: absolute; inset: 0; border-radius: 50%; background: radial-gradient(circle, rgba(0,0,0,0.12), rgba(0,0,0,0.42)); border: 2px solid rgba(240,200,115,0.45); }
.stick .knob { position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #f0d9a0, #9b6c2a 70%); border: 2px solid #f7d993; box-shadow: 0 4px 12px rgba(0,0,0,0.5); }
#guardStick .knob { background: radial-gradient(circle at 35% 30%, #dfe6ee, #6d7884 70%); border-color: #e9eef3; }
.stick .label { position: absolute; top: -22px; width: 100%; text-align: center; font-family: Cinzel, serif; font-size: 13px; color: rgba(243,233,214,0.75); }
#guardGauge { position: fixed; left: 50%; bottom: max(14px, env(safe-area-inset-bottom)); transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 4px; pointer-events: none; z-index: 5; }
#guardGauge .dial { position: relative; width: 64px; height: 64px; border-radius: 50%; border: 2px solid rgba(240,200,115,0.35); background: rgba(0,0,0,0.35); }
#guardGauge .blade { position: absolute; left: 6px; right: 6px; top: 50%; height: 5px; margin-top: -2.5px; border-radius: 3px; background: #8a8f96; transition: background 0.1s; }
#guardGauge.up .blade { background: #e6ecf2; box-shadow: 0 0 8px rgba(255,255,255,0.6); }
#guardGauge.good .blade { background: #9fd17a; box-shadow: 0 0 12px #9fd17a; }
#guardGauge .state { font-family: Cinzel, serif; font-size: 12px; color: rgba(243,233,214,0.8); text-shadow: 0 1px 3px #000; }
body.in-free #btnYield { pointer-events: auto; }
.free-opts .tilt-opts { margin-top: 6px; }
label.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; }
label.check input[type=checkbox] { width: auto; margin: 0; }
label.check select { margin: 0; }
.hint.small { font-size: 14px; }

body.in-free #hud { z-index: 6; }
.home-link { display: block; text-align: center; color: var(--muted); font-size: 15px; margin-top: 4px; }

/* bot trap: off-screen, never seen by people */
.hp-field { position: absolute !important; left: -9999px !important; width: 1px; height: 1px; overflow: hidden; }
/* levels */
.choices.levels .choice small { display: block; line-height: 1.3; margin-top: 2px; }
/* lobby + rankings */
.players.lobby { max-height: 30vh; margin-top: 4px; }
.players li .vs { color: var(--gold-2); font-weight: 600; }
.players li .busy { color: var(--warn); }
.ranks { list-style: none; padding: 0; margin: 10px 0 0; max-height: 50vh; overflow-y: auto; }
.ranks li { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 10px; padding: 9px 6px; border-bottom: 1px solid rgba(217,164,74,0.15); }
.ranks li.me { background: rgba(217,164,74,0.14); border-radius: 8px; }
.ranks .pos { font-family: Cinzel, serif; font-weight: 700; color: var(--gold-2); text-align: center; }
.ranks .who small { display: block; color: var(--muted); }
.ranks .rating { font-family: Cinzel, serif; font-weight: 700; }
.ranks .empty { display: block; color: var(--muted); text-align: center; }
.result-line { font-size: 18px; color: var(--ink); margin: 0 0 6px; }
.result-line .up { color: var(--good); } .result-line .down { color: #ff8a7a; }
.ranks .who { text-align: left; }
.hud-icon { position: absolute; top: max(56px, calc(env(safe-area-inset-top) + 46px)); pointer-events: auto; width: 40px; font-size: 17px; padding: 6px 0; }
#btnRotate { right: 88px; }
#btnFull { right: 136px; display: none; }
body.can-fullscreen #btnFull { display: block; }

/* Menus taller than the screen (phone held sideways) must scroll all the way to the
   top: centre with auto margins instead of align-items, which cuts off the top. */
.screen { align-items: flex-start; touch-action: pan-x pan-y; -webkit-overflow-scrolling: touch; }
.screen > .card { margin-top: auto; margin-bottom: auto; }
@media (max-width: 760px) { .screen.side { align-items: flex-start; } .screen.side > .card { margin-bottom: 0; } }
.overlay { align-items: flex-start; overflow-y: auto; touch-action: pan-x pan-y; }
.overlay > .card { margin-top: auto; margin-bottom: auto; }
.view-opts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 8px 0 4px; }
.view-opt { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; background: rgba(0,0,0,0.35); color: var(--ink); border-color: var(--line); font-family: inherit; padding: 12px 8px; }
.view-opt svg { width: 84px; height: 84px; }
.view-opt b { font-family: Cinzel, serif; color: var(--gold-2); font-size: 16px; }
.view-opt small { color: var(--muted); font-weight: 400; font-size: 13px; }
.view-opt.on { background: rgba(217,164,74,0.22); border-color: var(--gold); box-shadow: inset 0 0 0 1px var(--gold); }
.view-row { color: var(--muted); margin: 14px 0 0; }
.view-row b { color: var(--ink); }
.view-row .link { width: auto; margin: 0 0 0 6px; padding: 0; font-size: 15px; }
#viewPicker { z-index: 40; }
#guardStick .blade { position: absolute; left: 10px; right: 10px; top: 50%; height: 5px; margin-top: -2.5px; border-radius: 3px;
  background: linear-gradient(90deg, #8d96a0, #f4f7fa, #8d96a0); box-shadow: 0 0 8px rgba(255,255,255,0.5); opacity: 0; transition: opacity 0.1s; }
#guardStick.swept .ring { animation: swept 0.45s ease-out; }
@keyframes swept { 0% { box-shadow: 0 0 0 0 rgba(255,214,140,0.9); border-color: #ffd68c; } 100% { box-shadow: 0 0 0 22px rgba(255,214,140,0); } }
#coach { position: absolute; left: 50%; top: 58px; transform: translateX(-50%); width: min(560px, calc(100% - 32px)); pointer-events: auto;
  background: rgba(20, 15, 10, 0.86); border: 1px solid var(--gold); border-radius: 12px; padding: 10px 14px; text-align: center; z-index: 7;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5); }
#coach .coach-step { font-family: Cinzel, serif; font-size: 12px; letter-spacing: 1px; color: var(--gold-2); text-transform: uppercase; }
#coach .coach-text { font-size: 16px; line-height: 1.35; margin-top: 3px; }
#coach .coach-text b { color: #fff3d6; }
#coach .coach-note { color: var(--good); font-weight: 600; }
#coach .coach-end button { margin-top: 8px; padding: 9px 10px; font-size: 14px; }
body #coach:not([hidden]) ~ #banner { top: 34%; }
@media (max-height: 500px) { #coach { top: 46px; padding: 6px 10px; } #coach .coach-text { font-size: 14px; } }
body.practicing #banner { top: 40%; }
body.practicing #popups { top: 52%; }
#coach { top: 62px; }
@media (max-aspect-ratio: 1/1) { #coach { top: 100px; } body.practicing #banner { top: 46%; } }
@media (max-height: 500px) { #coach { top: 60px; } }
.note-bar { font-size: 14px; color: var(--muted); text-align: center; margin: -8px 0 10px; }
.note-bar .link { display: inline; width: auto; margin: 0 0 0 4px; padding: 0; font-size: 14px; color: var(--gold-2); }
.tabs .tab { font-size: 14px; padding: 9px 4px; }
.players li .tag-guest { font-size: 11px; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; margin-left: 4px; color: var(--muted); }
