:root {
  --bg: #0b0d17;
  --bg2: #141829;
  --panel: #1b2038;
  --line: #2b3153;
  --text: #eef0ff;
  --muted: #9aa0c4;
  --accent: #ffd84a;
  --wire: #3a4166;
  --wire-on: var(--accent);
  --glitch: #c45cff;
  --good: #7af5c8;
  --bad: #ff6b8a;
  --radius: 18px;
  --gap: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
body { overscroll-behavior: none; }
button { font: inherit; color: inherit; }

.app {
  min-height: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: var(--gap);
  background:
    radial-gradient(1200px 500px at 50% -200px, color-mix(in srgb, var(--accent) 18%, transparent), transparent 70%),
    var(--bg);
}

h1, h2, h3 { margin: 0; letter-spacing: .5px; }
h1 { font-size: 28px; }
h2 { font-size: 20px; }
h3 { font-size: 14px; text-transform: uppercase; color: var(--muted); letter-spacing: 1.2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 54px; padding: 12px 16px; border-radius: 16px;
  border: 1px solid var(--line); background: var(--bg2); font-weight: 700; font-size: 17px;
  cursor: pointer; transition: transform .08s ease, background .15s ease;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: #141414; border-color: transparent; box-shadow: 0 6px 24px color-mix(in srgb, var(--accent) 35%, transparent); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 40px; font-size: 14px; padding: 8px 12px; width: auto; }
.btn:disabled { opacity: .45; }

/* ---------- character select ---------- */
.chars { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.char {
  position: relative; border-radius: var(--radius); border: 2px solid var(--line); background: var(--panel);
  padding: 10px 6px 8px; display: flex; flex-direction: column; align-items: center; gap: 6px; cursor: pointer;
  transition: border-color .15s ease, box-shadow .15s ease, transform .1s ease;
}
.char img { height: 150px; width: auto; max-width: 100%; object-fit: contain; filter: drop-shadow(0 8px 14px rgba(0,0,0,.6)); }
.char .name { font-weight: 800; font-size: 18px; }
.char.selected { border-color: var(--c); box-shadow: 0 0 0 4px color-mix(in srgb, var(--c) 25%, transparent), 0 10px 30px color-mix(in srgb, var(--c) 30%, transparent); transform: translateY(-3px); }
.speech { min-height: 48px; font-style: italic; color: var(--text); }

.namebox { display: flex; align-items: center; gap: 10px; }
.namebox .name { font-size: 22px; font-weight: 800; flex: 1; }
.dice { width: 54px; height: 54px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2); font-size: 26px; cursor: pointer; }

/* ---------- home ---------- */
.hero { display: flex; gap: 14px; align-items: center; }
.hero img { height: 120px; width: auto; filter: drop-shadow(0 8px 14px rgba(0,0,0,.6)); }
.hero .title { font-size: 22px; font-weight: 800; line-height: 1.1; }
.code { display: inline-flex; align-items: center; gap: 6px; margin-top: 6px; padding: 6px 10px; border-radius: 10px; background: var(--bg2); border: 1px dashed var(--line); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 18px; letter-spacing: 3px; cursor: pointer; }
.stat { display: flex; gap: 14px; margin-top: 8px; font-size: 14px; color: var(--muted); }
.stat b { color: var(--text); }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions .btn { flex-direction: column; min-height: 86px; gap: 4px; }
.actions .btn .big { font-size: 28px; line-height: 1; }

.list { display: flex; flex-direction: column; gap: 8px; }
.item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 14px; background: var(--bg2); border: 1px solid var(--line); }
.avatar { width: 40px; height: 40px; border-radius: 12px; background: var(--panel); object-fit: cover; object-position: top; flex: none; }
.top .avatar { width: 28px; height: 28px; border-radius: 8px; }
.item .who { font-weight: 700; }
.item .sub { font-size: 13px; color: var(--muted); }
.tag { font-size: 12px; font-weight: 800; padding: 4px 8px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); white-space: nowrap; }
.tag.win { color: #141414; background: var(--good); border-color: transparent; }
.tag.lose { color: var(--bad); }
.tag.wait { color: var(--muted); }

.cores { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.core { aspect-ratio: 1; border-radius: 14px; background: var(--bg2); border: 1px solid var(--line); display: flex; align-items: center; justify-content: center; font-size: 26px; position: relative; }
.core.unknown { color: var(--line); font-size: 22px; font-weight: 800; }
.core .lv { position: absolute; right: 4px; bottom: 3px; font-size: 11px; font-weight: 800; color: var(--muted); }
.core.t-power { box-shadow: inset 0 0 0 2px #ffb02055; }
.core.t-signal { box-shadow: inset 0 0 0 2px #4fd1ff55; }
.core.t-coolant { box-shadow: inset 0 0 0 2px #7af5c855; }
.core.t-scrap { box-shadow: inset 0 0 0 2px #c9a27a55; }
.core.t-glitch { box-shadow: inset 0 0 0 2px #c45cff55; }

.top li { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 15px; }
.top li:last-child { border-bottom: 0; }
.top ol { margin: 0; padding: 0; list-style: none; }
.top .rank { width: 22px; color: var(--muted); font-weight: 800; }
.top .me { color: var(--accent); }

/* ---------- challenge form ---------- */
.codeinput {
  width: 100%; padding: 14px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2); color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 28px; letter-spacing: 8px; text-align: center; text-transform: uppercase; outline: none;
}
.codeinput:focus { border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { padding: 10px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg2); font-weight: 700; cursor: pointer; }
.chip.on { background: var(--accent); color: #141414; border-color: transparent; }
.error { color: var(--bad); font-weight: 700; min-height: 20px; }

/* ---------- puzzle ---------- */
.hud { display: flex; justify-content: space-between; align-items: center; }
.hud .n { font-size: 24px; font-weight: 800; }
.hud .l { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

.board { display: grid; gap: 4px; padding: 8px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line); touch-action: manipulation; user-select: none; }
.tile { aspect-ratio: 1; border-radius: 10px; background: var(--bg2); position: relative; overflow: hidden; cursor: pointer; }
.tile.port { background: transparent; cursor: default; }
.tile svg { width: 100%; height: 100%; display: block; }
.tile .rot { transform-origin: 32px 32px; transition: transform .14s ease-out; }
.tile path, .tile circle.hub { stroke: var(--wire); fill: none; stroke-width: 11; stroke-linecap: round; }
.tile circle.hub { fill: var(--wire); stroke: none; }
.tile.on path { stroke: var(--wire-on); filter: drop-shadow(0 0 6px var(--wire-on)); }
.tile.on circle.hub { fill: var(--wire-on); filter: drop-shadow(0 0 6px var(--wire-on)); }
.tile.locked { background: #1a1230; cursor: not-allowed; }
.tile.locked path { stroke: var(--glitch); stroke-dasharray: 6 6; opacity: .8; }
.tile.locked circle.hub { fill: var(--glitch); }
.tile.locked::after { content: '✖'; position: absolute; right: 5px; top: 2px; font-size: 12px; color: var(--glitch); }
.tile.shake { animation: shake .25s ease; }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
.port .lamp { fill: var(--wire); }
.port.on .lamp { fill: var(--wire-on); filter: drop-shadow(0 0 10px var(--wire-on)); }
.port .stem { stroke: var(--wire); stroke-width: 11; stroke-linecap: round; }
.port.on .stem { stroke: var(--wire-on); filter: drop-shadow(0 0 6px var(--wire-on)); }
.board.solved .tile.on { animation: pulse .9s ease infinite alternate; }
@keyframes pulse { to { filter: brightness(1.25); } }

.tip { font-size: 14px; color: var(--muted); text-align: center; }

/* ---------- overlay ---------- */
.overlay { position: fixed; inset: 0; background: rgba(5,6,12,.85); display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 10; }
.card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--line); border-radius: 24px; padding: 22px; text-align: center; display: flex; flex-direction: column; gap: 12px; animation: pop .25s ease; }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.bigcore { font-size: 84px; line-height: 1; filter: drop-shadow(0 10px 20px rgba(0,0,0,.6)); }
.stars { font-size: 32px; letter-spacing: 4px; }
.stars .off { opacity: .2; }
.typetag { display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 800; color: #141414; }

.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: #141414; padding: 10px 16px; border-radius: 999px; font-weight: 700; z-index: 20; animation: pop .2s ease; }

.footer { text-align: center; font-size: 12px; color: var(--muted); padding-top: 10px; }
.footer button { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; padding: 0; font-size: 12px; }

/* ---------- v0.2 ---------- */
.portrait { position: relative; flex: none; }
.companion { position: absolute; right: -6px; bottom: 4px; width: 40px; height: 40px; border-radius: 12px; background: var(--panel); border: 2px solid var(--line); display: flex; align-items: center; justify-content: center; font-size: 22px; box-shadow: 0 6px 14px rgba(0,0,0,.5); animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-4px); } }
.bdg { font-size: .85em; }
.levelbar { height: 8px; border-radius: 999px; background: var(--bg2); border: 1px solid var(--line); overflow: hidden; margin: 6px 0 4px; }
.levelbar div { height: 100%; background: var(--accent); border-radius: 999px; transition: width .4s ease; box-shadow: 0 0 10px var(--accent); }
.speech.small { font-size: 14px; padding: 0 4px; min-height: 0; }
.stat.panel { justify-content: space-around; margin-top: 0; padding: 10px 14px; }
.actions .btn .sub { font-size: 11px; font-weight: 600; color: var(--muted); }
.actions .btn.primary .sub { color: #333; }
.btn.glow { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 30%, transparent); }
.btn.diff { justify-content: space-between; text-align: left; }
.btn.diff.locked { opacity: .5; }

.core.fav { outline: 3px solid var(--accent); outline-offset: -1px; }
.core.tier-silver { background: linear-gradient(160deg, #2a2f4a, #3a4166); border-color: #9aa0c4; }
.core.tier-gold { background: linear-gradient(160deg, #3a2f10, #5a4a1a); border-color: #ffd84a; }
.bigcore.tier-silver { filter: drop-shadow(0 0 18px #9aa0c4); }
.bigcore.tier-gold { filter: drop-shadow(0 0 22px #ffd84a); }
.typetag.tier-silver { background: #9aa0c4; }
.typetag.tier-gold { background: #ffd84a; }

.swatches { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.swatch { aspect-ratio: 1; border-radius: 16px; border: 2px solid var(--line); background: var(--c); cursor: pointer; position: relative; box-shadow: inset 0 0 0 4px var(--panel); }
.swatch.on { border-color: var(--text); box-shadow: inset 0 0 0 4px var(--panel), 0 0 16px var(--c); }
.swatch.locked { background: color-mix(in srgb, var(--c) 25%, var(--bg2)); }
.swatch .lk, .badge .lk { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 800; color: var(--text); text-align: center; line-height: 1.2; }
.badge .lk { inset: auto 2px 2px auto; font-size: 9px; color: var(--muted); }
.badges { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.badge { aspect-ratio: 1; border-radius: 16px; border: 2px solid var(--line); background: var(--bg2); font-size: 26px; cursor: pointer; position: relative; }
.badge.on { border-color: var(--accent); box-shadow: 0 0 14px color-mix(in srgb, var(--accent) 50%, transparent); }
.badge.locked { opacity: .45; font-size: 20px; }
.sel { flex: 1; min-height: 48px; padding: 10px 12px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2); color: var(--text); font: inherit; font-weight: 700; font-size: 16px; }
.hud .btn.small { min-width: 44px; padding: 8px; }
.nameinput { flex: 1; min-width: 0; min-height: 54px; padding: 10px 14px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2); color: var(--text); font: inherit; font-weight: 800; font-size: 20px; outline: none; }
.nameinput:focus { border-color: var(--accent); }

/* ---------- v0.3: layout fills a 390×800 phone ---------- */
.app { min-height: 100dvh; }
.screen-puzzle { gap: 10px; }
.screen-puzzle .buddy { flex: 1; align-items: center; gap: 16px; padding: 14px 16px; }
.screen-puzzle .buddy img { height: clamp(96px, 24vh, 200px); width: auto; max-width: 38%; object-fit: contain; object-position: bottom; background: none; filter: drop-shadow(0 10px 18px rgba(0,0,0,.6)); }
.screen-puzzle .buddy .say { font-size: 18px; font-weight: 700; line-height: 1.3; }
.screen-puzzle .buddy .grow { position: relative; padding: 12px 14px; border-radius: 16px; background: var(--bg2); border: 1px solid var(--line); }
.screen-puzzle .buddy .grow::before { content: ''; position: absolute; left: -8px; top: 50%; width: 14px; height: 14px; background: var(--bg2); border-left: 1px solid var(--line); border-bottom: 1px solid var(--line); transform: translateY(-50%) rotate(45deg); }
.screen-select .carousel { flex: 1; }

/* carousel */
.carousel { display: flex; gap: 12px; overflow-x: auto; scroll-snap-type: x mandatory; padding: 10px calc(50% - 36vw) 14px; margin: 0 -16px; scrollbar-width: none; -webkit-overflow-scrolling: touch; align-items: stretch; min-height: 300px; }
.carousel::-webkit-scrollbar { display: none; }
.carousel .char { flex: 0 0 72vw; max-width: 330px; scroll-snap-align: center; padding: 14px 10px 12px; justify-content: flex-end; }
.carousel .char img { height: min(300px, 38vh); }
.carousel .char .name { font-size: 22px; }
.carousel .char.selected { transform: none; }
.dots { display: flex; justify-content: center; gap: 6px; }
.dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--line); transition: background .2s ease, transform .2s ease; }
.dots span.on { background: var(--accent); transform: scale(1.3); }

/* buddy commentary */
.buddy { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--line); min-height: 86px; }
.buddy img { height: 72px; width: 56px; object-fit: cover; object-position: top; border-radius: 12px; background: var(--bg2); }
.buddy .say { font-size: 15px; font-weight: 600; line-height: 1.3; }
.buddy .say.pop { animation: saypop .25s ease; }
@keyframes saypop { from { opacity: 0; transform: translateY(4px); } }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; justify-content: center; font-size: 12px; }
.hunt { color: #ff4d6d; font-weight: 800; }

/* special tiles */
.board { position: relative; }
.tile .mark { position: absolute; left: 3px; top: 1px; font-size: 12px; pointer-events: none; filter: drop-shadow(0 1px 2px rgba(0,0,0,.8)); }
.tile.k-bonus .mark { font-size: 14px; left: 50%; top: 50%; transform: translate(-50%, -50%); }
.tile.k-bonus.on .mark { animation: sparkle .9s ease infinite alternate; }
@keyframes sparkle { from { transform: translate(-50%, -50%) scale(1); filter: drop-shadow(0 0 2px #fff); } to { transform: translate(-50%, -50%) scale(1.25); filter: drop-shadow(0 0 10px #fff); } }
.tile.k-bonus.on circle.hub { fill: #fff; }
.tile.hidden svg { opacity: 0; }
.tile.hidden { background: repeating-linear-gradient(135deg, #1b2038 0 6px, #141829 6px 12px); }
.tile.hidden .mark { left: 50%; top: 50%; transform: translate(-50%, -50%); font-size: 20px; }
.tile.revealing { animation: flip .45s ease; }
@keyframes flip { from { transform: rotateY(90deg); } to { transform: rotateY(0); } }
.tile.k-glitch .mark { color: #fff; }
.tile.zapping { animation: zap .6s ease; }
.tile.zapped { animation: zapped .6s ease; box-shadow: 0 0 0 2px #fff inset; }
@keyframes zap { 0%, 100% { filter: none; } 20%, 60% { filter: brightness(2.2) saturate(2); } 40%, 80% { filter: brightness(.6); } }
@keyframes zapped { 0% { transform: scale(1); } 30% { transform: scale(1.12) rotate(-4deg); } 60% { transform: scale(.96) rotate(3deg); } 100% { transform: scale(1); } }
.tile.corrupting { animation: corrupt .7s ease; }
@keyframes corrupt { 0% { box-shadow: 0 0 0 0 #c45cff; } 50% { box-shadow: 0 0 0 6px #c45cff88; transform: scale(1.08); } 100% { box-shadow: 0 0 0 0 transparent; } }

/* hunter drone */
.hunter { position: absolute; left: 0; top: 0; pointer-events: none; z-index: 2; transition: left .35s cubic-bezier(.4,1.4,.6,1), top .35s cubic-bezier(.4,1.4,.6,1); filter: drop-shadow(0 0 8px #ff4d6d99); animation: hover 1.6s ease-in-out infinite; }
.hunter svg { width: 100%; height: 100%; }
@keyframes hover { 50% { transform: translateY(-3px); } }
.hunter.flee { transition: transform .8s ease-in, opacity .8s ease-in; transform: translate(60vw, -60vh) rotate(40deg) scale(.4); opacity: 0; animation: none; }

/* ---------- v0.4: home fits 390×800 without scrolling ---------- */
.screen-home { gap: 8px; max-height: 100dvh; overflow: hidden; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
.screen-home .hero.compact img { height: 96px; }
.screen-home .hero.compact .title { font-size: 20px; }
.screen-home .hero.compact .code { margin-top: 4px; padding: 4px 8px; font-size: 15px; }
.screen-home .levelbar { margin: 4px 0 2px; height: 6px; }
.screen-home .speech.small { font-size: 13px; }
.screen-home .actions { gap: 8px; }
.screen-home .actions .btn { min-height: 66px; padding: 8px 10px; font-size: 15px; gap: 2px; }
.screen-home .actions .btn .big { font-size: 22px; }
.screen-home .stat.panel { padding: 8px 12px; font-size: 13px; }
.screen-home .panel.compact { padding: 10px 12px; }
.screen-home .panel.compact h3 { font-size: 12px; }
.screen-home .cores { gap: 6px; }
.screen-home .core { font-size: 20px; border-radius: 10px; }
.screen-home .core .lv { font-size: 9px; }
.screen-home .item { padding: 6px 10px; }
.screen-home .item .avatar { width: 32px; height: 32px; border-radius: 9px; }
.screen-home .item .who { font-size: 14px; }
.screen-home .item .sub { font-size: 12px; }
.screen-home .top li { padding: 4px 0; font-size: 14px; }
.screen-home .top .avatar { width: 26px; height: 26px; }
.screen-home .footer { padding-top: 4px; margin-top: auto; }
.screen-home .companion { width: 32px; height: 32px; font-size: 18px; right: -4px; bottom: 0; }
@media (max-height: 740px) { .screen-home { max-height: none; overflow: visible; } }

/* ---------- v0.4.1: start screen fixed height, carousel fills the remaining space ---------- */
.screen-select { height: 100dvh; max-height: 100dvh; overflow: hidden; gap: 8px; }
.screen-select h1 { font-size: 24px; }
.screen-select .carousel { flex: 1 1 auto; min-height: 0; padding-top: 6px; padding-bottom: 6px; }
.screen-select .carousel .char { height: 100%; padding: 10px 10px 8px; justify-content: flex-end; }
.screen-select .carousel .char img { flex: 1 1 auto; min-height: 0; height: auto; width: auto; max-width: 100%; object-fit: contain; }
.screen-select .carousel .char .name { font-size: 20px; flex: none; }
.screen-select .panel { padding: 10px 12px; }
.screen-select .speech { min-height: 0; font-size: 14px; }
.screen-select .panel h3 { margin-top: 6px !important; }
.screen-select .nameinput { min-height: 46px; font-size: 18px; }
.screen-select .dice { width: 46px; height: 46px; font-size: 22px; }
.screen-select .btn.primary { min-height: 50px; }
.screen-select .error:empty { display: none; }
.screen-select .footer { padding-top: 0; }
@media (max-height: 740px) { .screen-select { height: auto; max-height: none; overflow: visible; } .screen-select .carousel { min-height: 300px; } .screen-select .carousel .char img { height: 220px; flex: none; } }
