/* Thien Loi va Chuyen Do Tri Thuc - giao dien */
:root {
  --ink: #1b2236;
  --panel: rgba(24, 34, 58, 0.92);
  --panel-line: #e8c46a;
  --gold: #ffd25a;
  --red: #c8352b;
  --green: #22a35a;
  --green-dark: #167741;
  --txt: #fff8e6;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --bar-h: 64px;
  --btn-h: 52px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: #10182a; color: var(--txt); overflow: hidden; }
body { -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; }
.hidden { display: none !important; }

#app { position: fixed; inset: 0; overflow: hidden; }
#stage { position: absolute; inset: 0; overflow: hidden; }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
#flash { position: absolute; inset: 0; pointer-events: none; background: #fffbe8; opacity: 0; }

/* HUD */
.hud {
  position: absolute; top: calc(6px + var(--sat)); left: calc(8px + var(--sal)); right: calc(8px + var(--sar));
  display: flex; gap: 6px; align-items: center; pointer-events: none; z-index: 5;
}
.chip {
  display: flex; align-items: center; gap: 6px; height: 36px; padding: 0 10px; border-radius: 12px;
  background: rgba(20, 28, 50, 0.72); border: 1.5px solid rgba(232, 196, 106, 0.75);
  font-size: 15px; white-space: nowrap; text-shadow: 0 1px 0 rgba(0,0,0,.4);
}
.chip .k { opacity: .85; font-size: 13px; }
.chip b { font-variant-numeric: tabular-nums; color: var(--gold); }
.chip-level { font-weight: 700; }
.chip-energy { flex: 1 1 auto; min-width: 120px; max-width: 280px; }
.ebar { flex: 1; height: 10px; border-radius: 6px; background: rgba(255,255,255,.18); overflow: hidden; }
.ebar i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, #ffd25a, #8be06b); transform-origin: left; }
.icon-btn {
  pointer-events: auto; width: 44px; height: 44px; margin-left: auto; border-radius: 12px; border: 1.5px solid rgba(232,196,106,.85);
  background: rgba(20, 28, 50, 0.8); display: grid; place-items: center; cursor: pointer; flex: none;
}
#btnFull { margin-left: auto; }
#btnPause { margin-left: 0; }
.ico { display: inline-block; width: 20px; height: 20px; background: currentColor; -webkit-mask: var(--m) center / contain no-repeat; mask: var(--m) center / contain no-repeat; }
.ico-pause { --m: url("../assets/ui/i_pause.svg"); }
.ico-full { --m: url("../assets/ui/i_full.svg"); }
.ico-swap { --m: url("../assets/ui/i_swap.svg"); }
.ico-boat { --m: url("../assets/ui/i_boat.svg"); width: 26px; height: 26px; }

/* Thanh quyen nang */
.controls {
  position: absolute; z-index: 6; left: calc(8px + var(--sal)); right: calc(8px + var(--sar)); bottom: calc(8px + var(--sab));
  display: flex; gap: 8px; align-items: stretch; height: var(--btn-h);
}
.slots { display: flex; gap: 8px; flex: 1 1 auto; min-width: 0; }
.pw {
  position: relative; flex: 0 1 150px; min-width: 64px; height: 100%; border-radius: 14px; cursor: pointer; overflow: hidden;
  border: 2px solid rgba(255, 230, 160, 0.9); background: linear-gradient(180deg, #34508a, #22355f);
  display: flex; align-items: center; justify-content: center; gap: 6px; padding: 0 8px;
  box-shadow: 0 3px 0 rgba(0,0,0,.35); touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.pw img { width: 34px; height: 34px; flex: none; pointer-events: none; }
.pw .lb { font-weight: 700; font-size: 15px; line-height: 1.1; text-align: left; }
.pw .cost { position: absolute; right: 5px; top: 2px; font-size: 11px; opacity: .85; color: var(--gold); }
.pw .cd { position: absolute; inset: 0; background: rgba(8, 12, 24, .62); transform-origin: bottom; transform: scaleY(0); pointer-events: none; }
.pw .cdt { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 800; font-size: 18px; pointer-events: none; text-shadow: 0 1px 2px #000; }
.pw.is-off { filter: grayscale(.65) brightness(.8); }
.pw.is-low .cost { color: #ff8f7a; }
.pw:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.35); }
.pw.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.group-btn {
  flex: none; min-width: 64px; height: 100%; border-radius: 14px; border: 2px solid rgba(255,230,160,.9); cursor: pointer;
  background: #4a3a78; display: flex; align-items: center; justify-content: center; gap: 4px; font-weight: 700; padding: 0 8px;
}
.release-btn {
  flex: none; margin-left: auto; min-width: 150px; height: 100%; border-radius: 16px; cursor: pointer; border: 2.5px solid #fff3c4;
  background: linear-gradient(180deg, #31c46d, var(--green-dark)); font-weight: 900; font-size: 20px; letter-spacing: .5px;
  display: flex; align-items: center; justify-content: center; gap: 8px; box-shadow: 0 4px 0 #0c4a28; padding: 0 14px;
}
.release-btn:disabled { filter: grayscale(.6) brightness(.85); cursor: default; }
.release-btn.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 4px 0 #0c4a28, 0 0 0 7px rgba(255, 236, 140, .55); } }
button:focus-visible, input:focus-visible { outline: 3px solid #7fd3ff; outline-offset: 2px; }

/* Bong bong thoai */
.bubble {
  position: absolute; z-index: 4; max-width: min(300px, 46vw); padding: 7px 11px; border-radius: 14px; pointer-events: none;
  background: #fffdf3; color: var(--ink); border: 2px solid #3a2f25; font-size: 15px; line-height: 1.3;
  box-shadow: 0 3px 0 rgba(0,0,0,.25); transform-origin: bottom center; animation: pop .22s ease-out;
}
.bubble .who { display: block; font-size: 12px; color: #a0442c; }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.hint {
  position: absolute; z-index: 4; left: 8px; right: 8px; top: calc(52px + var(--sat));
  display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; pointer-events: none;
}
.hint span { background: rgba(255, 253, 243, .94); color: var(--ink); border-radius: 12px; padding: 5px 10px; font-size: 14px; }
.hint b { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; background: var(--red); color: #fff; margin-right: 4px; font-size: 12px; }
.wait-tip {
  position: absolute; z-index: 4; right: calc(12px + var(--sar)); bottom: calc(var(--btn-h) + 18px + var(--sab));
  background: rgba(255,253,243,.95); color: var(--ink); padding: 6px 10px; border-radius: 12px; font-size: 14px; max-width: 280px; pointer-events: none;
}

/* Man hinh / panel */
.screen { position: absolute; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center; padding: calc(10px + var(--sat)) calc(10px + var(--sar)) calc(10px + var(--sab)) calc(10px + var(--sal)); overflow: auto; }
.screen.dim { background: rgba(8, 12, 24, .55); }
.panel { background: var(--panel); border: 2px solid var(--panel-line); border-radius: 18px; padding: 14px 18px; width: min(480px, 100%); max-height: 100%; overflow: auto; margin: auto; }
.panel.wide { width: min(900px, 100%); }
.panel h2 { margin: 0 0 10px; font-size: 22px; color: var(--gold); }
.panel h3 { margin: 14px 0 6px; font-size: 16px; color: var(--gold); }
.panel p { margin: 6px 0; line-height: 1.45; }
.row { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.row.wrap { flex-wrap: wrap; }
.col { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.btn {
  min-height: 46px; padding: 0 18px; border-radius: 14px; border: 2px solid rgba(255,230,160,.9); cursor: pointer;
  background: #2d4677; font-weight: 700; font-size: 16px;
}
.btn.small { min-height: 44px; font-size: 14px; padding: 0 12px; }
.btn-primary { background: linear-gradient(180deg, #e2483b, #a72a20); border-color: #ffe08a; }
.btn-ghost { background: transparent; }
.btn-danger { background: #6b2a2a; }
.note { font-size: 13px; opacity: .9; }

/* Trang chu */
#menu { background: linear-gradient(90deg, rgba(10,16,30,.5), rgba(10,16,30,0) 60%); justify-content: flex-start; align-items: flex-start; padding-left: calc(4vw + var(--sal)); padding-top: calc(4vh + var(--sat)); }
.menu-card { text-align: left; max-width: min(560px, 58vw); margin: 0; }
.menu-card .menu-actions { justify-content: flex-start; }
.menu-card .sub { margin-left: 0; }
.menu-card h1 { margin: 0; line-height: 1.05; text-shadow: 0 3px 0 #6b1b14, 0 6px 14px rgba(0,0,0,.45); }
.menu-card .t1 { display: block; font-size: clamp(34px, 8vh, 64px); color: var(--gold); letter-spacing: 1px; }
.menu-card .t2 { display: block; font-size: clamp(20px, 5vh, 36px); color: #fff; }
.menu-card .sub { margin: 8px auto 14px; font-size: 15px; max-width: 460px; text-shadow: 0 1px 3px #000; }
.menu-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.menu-actions .btn-primary { min-width: 200px; font-size: 19px; }
.menu-info { font-size: 13px; text-shadow: 0 1px 3px #000; min-height: 1em; }

/* Hanh trinh */
.level-list { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.level-list li { background: rgba(255,255,255,.06); border: 1.5px solid rgba(232,196,106,.5); border-radius: 14px; padding: 10px 8px; text-align: center; display: flex; flex-direction: column; gap: 6px; }
.level-list .lv-n { font-size: 12px; opacity: .8; }
.level-list .lv-name { font-weight: 800; font-size: 17px; color: var(--gold); }
.level-list .lv-st { font-size: 13px; min-height: 18px; }
.level-list li.locked { opacity: .55; }
.level-list .btn { min-height: 44px; padding: 0 8px; }

/* Bo suu tap */
.coll-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 6px; }
.coll-item { display: flex; align-items: center; gap: 6px; padding: 6px; border-radius: 10px; background: rgba(255,255,255,.07); font-size: 13px; min-height: 44px; }
.coll-item img { width: 26px; height: 26px; }
.coll-item.off { opacity: .35; }
.coll-item small { display: block; opacity: .75; }

/* Cai dat */
.set-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; }
.sw, .rng { display: flex; align-items: center; gap: 8px; min-height: 44px; font-size: 15px; }
.sw input { width: 22px; height: 22px; accent-color: #e2483b; }
.rng { flex-direction: column; align-items: stretch; justify-content: center; gap: 2px; }
.rng input { width: 100%; accent-color: #ffd25a; }

.guide-steps { padding-left: 22px; margin: 0; }
.guide-steps li { margin-bottom: 8px; line-height: 1.4; }
.guide-steps li b:first-child { display: block; color: var(--gold); }

.res-line { font-size: 18px; text-align: center; }
.res-scores { display: flex; gap: 10px; justify-content: center; }
.res-scores div { background: rgba(255,255,255,.07); border-radius: 12px; padding: 8px 14px; text-align: center; }
.res-scores span { display: block; font-size: 13px; opacity: .85; }
.res-scores b { font-size: 24px; color: var(--gold); }

#ending { align-items: flex-end; pointer-events: none; }
.ending-card { pointer-events: auto; background: rgba(255, 252, 240, .94); color: var(--ink); border-radius: 18px; padding: 12px 18px; max-width: 760px; margin: 0 auto 4px; text-align: center; border: 2px solid #c79a3a; animation: pop .5s ease-out; }
.ending-quote { font-size: clamp(16px, 3.2vh, 22px); font-weight: 700; margin: 0 0 6px; line-height: 1.4; color: #6b2416; }
.ending-card .btn { color: #fff; }
.ending-card .btn-ghost { color: var(--ink); border-color: #c79a3a; }

.rotate { position: absolute; z-index: 8; left: 0; right: 0; bottom: calc(8px + var(--sab)); display: flex; gap: 8px; align-items: center; justify-content: center; padding: 8px; font-size: 14px; }
.toast { position: absolute; z-index: 30; left: 50%; top: calc(56px + var(--sat)); transform: translateX(-50%); background: rgba(255,253,243,.97); color: var(--ink); padding: 8px 14px; border-radius: 12px; font-size: 14px; max-width: 90%; text-align: center; }
.loading { position: absolute; inset: 0; z-index: 40; background: #14203a; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid rgba(255,255,255,.2); border-top-color: var(--gold); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Man hinh ngang thap */
@media (max-height: 400px) and (orientation: landscape) {
  :root { --btn-h: 46px; }
  .chip { height: 32px; font-size: 13px; padding: 0 7px; gap: 4px; }
  .chip .k { font-size: 11px; }
  .icon-btn { width: 44px; height: 44px; }
  .pw { padding: 0 5px; gap: 4px; }
  .pw img { width: 28px; height: 28px; }
  .pw .lb { font-size: 13px; }
  .release-btn { min-width: 118px; font-size: 17px; padding: 0 10px; }
  .panel { padding: 10px 14px; }
  .panel h2 { font-size: 19px; margin-bottom: 6px; }
  .level-list li { padding: 6px; gap: 3px; }
  .bubble { font-size: 13px; padding: 5px 9px; }
  .hint span { font-size: 12px; padding: 3px 8px; }
  .hint { top: calc(44px + var(--sat)); }
  .menu-card .t1 { font-size: 34px; }
  .menu-card .t2 { font-size: 21px; }
  .menu-card .sub { font-size: 13px; margin: 4px 0 8px; }
  .menu-actions .btn-primary { min-width: 150px; font-size: 17px; }
}
@media (max-width: 700px) and (orientation: landscape) {
  .chip-k-hide, .chip .k { display: none; }
  .chip-energy { min-width: 70px; }
  .pw .lb { font-size: 12px; }
  .pw img { width: 26px; height: 26px; }
}

/* Man hinh doc: canvas 16:9 o tren, dieu khien ben duoi */
@media (orientation: portrait) {
  #menu { justify-content: center; align-items: center; padding-left: 10px; background: rgba(10,16,30,.45); }
  .menu-card { text-align: center; }
  .menu-card .menu-actions { justify-content: center; }
  #app.mode-play, #app.mode-pause, #app.mode-release, #app.mode-arrival { display: flex; flex-direction: column; }
  #app.mode-play #stage, #app.mode-pause #stage, #app.mode-release #stage, #app.mode-arrival #stage {
    position: relative; inset: auto; flex: none; width: 100%; aspect-ratio: 16 / 9; margin-top: calc(56px + var(--sat)); overflow: visible;
  }
  .hud { top: calc(-50px); flex-wrap: nowrap; }
  #app.mode-play .controls, #app.mode-pause .controls, #app.mode-release .controls, #app.mode-arrival .controls {
    position: relative; left: auto; right: auto; bottom: auto; height: auto; margin: 10px calc(8px + var(--sar)) 0 calc(8px + var(--sal));
    flex-direction: column; gap: 8px;
  }
  .slots { flex-wrap: wrap; }
  .pw { flex: 1 1 calc(50% - 8px); height: 54px; }
  .group-btn { height: 48px; }
  .release-btn { margin-left: 0; height: 60px; width: 100%; }
  .wait-tip { right: 8px; bottom: 8px; }
  .level-list { grid-template-columns: 1fr; }
  .set-grid { grid-template-columns: 1fr; }
  .chip .k { display: none; }
  .hud { gap: 4px; left: 4px; right: 4px; }
  .chip { padding: 0 5px; font-size: 12px; height: 32px; }
  .chip-energy { min-width: 0; flex: 0 0 auto; }
  .chip-energy .ebar { display: none; }
  .icon-btn { width: 44px; height: 44px; }
  .hint { display: none !important; }
  .menu-card { max-width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  .bubble, .ending-card { animation: none; }
  .release-btn.pulse { animation: none; box-shadow: 0 4px 0 #0c4a28, 0 0 0 4px rgba(255,236,140,.6); }
}
