:root {
    --app-viewport-width: 100vw;
    --app-viewport-height: 100svh;
    --stage-scale: 1;
    --stage-x: 0px;
    --stage-y: 0px;
    color-scheme: dark;
    --ink: #f9f2df;
    --gold: #f7c85c;
  }

  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
  }

  html,
  body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
  }

  body {
    position: fixed;
    inset: 0;
    width: var(--app-viewport-width);
    height: var(--app-viewport-height);
    color: var(--ink);
    font-family: "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
    background:
      radial-gradient(circle at 50% 18%, rgba(78, 217, 255, 0.62), transparent 34%),
      linear-gradient(180deg, #20aee5 0%, #07557c 58%, #03283e 100%);
  }

  button {
    color: inherit;
    font: inherit;
  }

  .app-shell {
    position: relative;
    width: var(--app-viewport-width);
    height: var(--app-viewport-height);
    overflow: hidden;
    isolation: isolate;
    background: #05263a;
  }

  .app-shell::before {
    position: absolute;
    z-index: 0;
    inset: -16px;
    background:
      linear-gradient(90deg, rgba(2, 24, 37, 0.3), rgba(3, 42, 57, 0.08), rgba(2, 24, 37, 0.3)),
      url("../assets/lobby/home-background.webp") center 48% / cover no-repeat;
    filter: blur(9px) brightness(0.72) saturate(1.1);
    transform: scale(1.04);
    content: "";
    pointer-events: none;
  }

  .app-shell::after {
    position: absolute;
    z-index: 0;
    inset: 0;
    background: radial-gradient(circle at center, transparent 24%, rgba(2, 20, 31, 0.34) 72%, rgba(1, 10, 17, 0.72));
    content: "";
    pointer-events: none;
  }

  .game-stage {
    position: absolute;
    z-index: 1;
    left: 0;
    top: 0;
    width: 720px;
    height: 1280px;
    overflow: hidden;
    transform: translate3d(var(--stage-x), var(--stage-y), 0) scale(var(--stage-scale));
    transform-origin: 0 0;
  }

  #game-container {
    position: absolute;
    left: 0;
    top: 0;
    width: 720px;
    height: 1280px;
    overflow: hidden;
    isolation: isolate;
    border: 0;
    border-radius: 0;
    background: #33bce9;
    box-shadow: none;
    container-type: size;
    transform-origin: 0 0;
  }

  canvas {
    position: absolute;
    z-index: 1;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    outline: 0;
    touch-action: none;
  }

  #hud-layer {
    position: absolute;
    z-index: 12;
    inset: 0 0 auto;
    display: grid;
    grid-template-columns: 0.9fr 1.2fr 0.9fr;
    gap: 8px;
    padding: 14px 14px 0;
    transition: opacity 180ms ease;
    pointer-events: none;
  }

  .hud-chip {
    position: relative;
    display: block;
    width: 100%;
    height: 56px;
    min-width: 0;
    padding: 0;
    overflow: visible;
    border: 0;
    border-radius: 0;
    color: #fff7d8;
    background: transparent center / contain no-repeat;
    box-shadow: none;
    backdrop-filter: none;
  }

  .hud-label {
    position: absolute;
    left: 76px;
    top: 10px;
    display: block;
    overflow: hidden;
    color: rgba(255, 240, 196, 0.72);
    font-size: 9px;
    font-weight: 800;
    letter-spacing: 0.11em;
    line-height: 1;
    text-overflow: ellipsis;
    text-transform: uppercase;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.72);
    white-space: nowrap;
  }

  .hud-value {
    display: block;
    margin: 0;
    overflow: hidden;
    color: #fff5ca;
    font-size: 23px;
    font-weight: 950;
    letter-spacing: -0.02em;
    line-height: 1;
    text-overflow: ellipsis;
    text-shadow: 0 2px 0 #5d2b14, 0 0 5px rgba(255, 224, 120, 0.22);
    white-space: nowrap;
  }

  #lives-value {
    position: absolute;
    left: 68px;
    top: 19px;
    margin: 0;
    color: #ff776d;
    font-size: 20px;
    letter-spacing: 0.06em;
    text-shadow: 0 2px 0 #71291e, 0 0 6px rgba(255, 104, 92, 0.28);
  }

  #progress-value {
    display: inline;
    margin: 0;
    color: rgba(255, 239, 191, 0.76);
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0;
    text-shadow: 0 2px 2px rgba(0, 0, 0, 0.72);
  }

  #combo-pill {
    position: absolute;
    z-index: 13;
    top: 78px;
    left: 20px;
    display: grid;
    width: 90px;
    height: 91px;
    place-items: center;
    padding: 18px 0 0;
    border: 0;
    border-radius: 0;
    opacity: 0;
    color: #fff4c4;
    font-size: 20px;
    font-weight: 900;
    letter-spacing: 0.04em;
    background: url("../assets/ui/arrgh-combo-badge-v1.webp") center / contain no-repeat;
    box-shadow: none;
    filter: drop-shadow(0 7px 5px rgba(0, 0, 0, 0.35));
    text-shadow: 0 2px 0 #602a15;
    transform: translateY(-5px);
    transition: opacity 180ms ease, transform 180ms ease;
    pointer-events: none;
  }

  #combo-pill.active {
    opacity: 1;
    transform: translateY(0);
  }

  #game-hint {
    position: absolute;
    z-index: 12;
    right: auto;
    bottom: 166px;
    left: 50%;
    display: grid;
    width: 420px;
    height: 130px;
    place-items: start center;
    padding: 28px 24px 0 154px;
    border: 0;
    border-radius: 0;
    opacity: 0;
    color: #fff3c2;
    font-size: 20px;
    font-weight: 700;
    line-height: 1.35;
    text-align: center;
    background: url("../assets/ui/arrgh-gesture-prompt-v1.webp") center / contain no-repeat;
    box-shadow: none;
    backdrop-filter: none;
    filter: drop-shadow(0 9px 6px rgba(0, 0, 0, 0.42));
    transform: translate(-50%, 10px) scale(0.96);
    transition: opacity 180ms ease, transform 180ms ease;
    text-shadow: 0 2px 0 #5c2815;
    pointer-events: none;
  }

  #announcement {
    position: absolute;
    z-index: 22;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 28px;
    opacity: 0;
    transition: opacity 180ms ease;
    pointer-events: none;
  }

  #announcement.active {
    opacity: 1;
  }

  .announcement-card {
    width: min(100%, 450px);
    padding: 22px 24px;
    border: 4px solid #f1bd4c;
    border-radius: 24px;
    color: #fff7d8;
    text-align: center;
    background:
      linear-gradient(180deg, rgba(173, 75, 34, 0.98), rgba(89, 37, 20, 0.98));
    box-shadow:
      inset 0 2px 0 rgba(255, 255, 255, 0.24),
      0 10px 0 rgba(69, 28, 13, 0.58),
      0 26px 55px rgba(3, 44, 60, 0.38);
    backdrop-filter: blur(12px);
  }

  .announcement-kicker {
    color: var(--gold);
    font-size: 10px;
    font-weight: 900;
    letter-spacing: 0.18em;
    text-transform: uppercase;
  }

  .announcement-title {
    margin-top: 8px;
    font-size: clamp(30px, 9cqw, 48px);
    font-weight: 950;
    letter-spacing: -0.05em;
  }

  .announcement-body {
    margin-top: 8px;
    color: rgba(255, 248, 221, 0.9);
    font-size: clamp(12px, 3.4cqw, 16px);
    font-weight: 700;
    line-height: 1.55;
  }

  .screen {
    position: absolute;
    z-index: 30;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 14px;
    opacity: 0;
    background:
      linear-gradient(180deg, rgba(0, 119, 176, 0.08), rgba(3, 54, 71, 0.45));
    transition: opacity 220ms ease;
    pointer-events: none;
  }

  .screen-panel {
    position: relative;
    width: min(100%, 590px);
    max-height: calc(100% - 4px);
    padding: clamp(20px, 5cqw, 34px);
    overflow: auto;
    color: #4a2c18;
    border: 4px solid #75401f;
    border-radius: clamp(22px, 6cqw, 34px);
    background:
      radial-gradient(circle at 50% 0%, rgba(255, 255, 255, 0.62), transparent 34%),
      linear-gradient(180deg, #fff1bd 0%, #f4d38c 62%, #dda860 100%);
    box-shadow:
      inset 0 0 0 3px #f5c85f,
      inset 0 0 36px rgba(123, 66, 29, 0.18),
      0 10px 0 rgba(79, 39, 17, 0.58),
      0 30px 65px rgba(3, 47, 63, 0.4);
    scrollbar-width: none;
  }

  .screen-panel::-webkit-scrollbar {
    display: none;
  }

  .screen-panel::before {
    position: absolute;
    inset: 10px;
    border: 2px dashed rgba(116, 63, 28, 0.25);
    border-radius: inherit;
    content: "";
    pointer-events: none;
  }

  .primary-button,
  .secondary-button {
    position: relative;
    display: block;
    width: 100%;
    min-height: 48px;
    padding: 12px 18px;
    border: 0;
    border-radius: 15px;
    cursor: pointer;
    font-size: clamp(13px, 3.6cqw, 17px);
    font-weight: 950;
    transition: transform 100ms ease, filter 100ms ease;
  }

  .primary-button {
    margin-top: 14px;
    color: #fff4c9;
    border: 3px solid #723217;
    background: linear-gradient(180deg, #f68a30 0%, #d94920 100%);
    box-shadow:
      inset 0 2px 0 rgba(255, 255, 255, 0.3),
      0 6px 0 #79351a,
      0 11px 19px rgba(105, 47, 21, 0.2);
    text-shadow: 0 2px 0 #873218;
  }

  .secondary-button {
    color: #fff4c9;
    border: 3px solid #5c2c18;
    background: linear-gradient(180deg, #9f5a2f, #663318);
    box-shadow: 0 5px 0 #482312;
  }

  .primary-button:active,
  .secondary-button:active,
  button.is-pressed {
    transform: translateY(2px) scale(0.985);
    filter: brightness(0.93);
  }

  .best-line {
    position: relative;
    margin-top: 9px;
    color: rgba(255, 255, 255, 0.46);
    font-size: clamp(8px, 2.4cqw, 10px);
    font-weight: 800;
    letter-spacing: 0.08em;
    text-align: center;
    text-transform: uppercase;
  }

  .best-line b {
    color: var(--gold);
  }

  .result-icon {
    position: relative;
    display: grid;
    width: 74px;
    height: 74px;
    place-items: center;
    padding: 4px;
    border: 0;
    border-radius: 50%;
    color: var(--gold);
    font-size: 28px;
    background: none;
  }

  .result-title {
    position: relative;
    margin-top: 12px;
    font-size: clamp(34px, 10cqw, 52px);
    font-weight: 1000;
    letter-spacing: -0.055em;
    line-height: 1;
    color: #7b391b;
    text-shadow: 0 2px 0 rgba(255, 255, 255, 0.6);
  }

  .result-copy {
    position: relative;
    margin-top: 10px;
    color: rgba(73, 42, 22, 0.82);
    font-size: clamp(11px, 3cqw, 14px);
    font-weight: 650;
    line-height: 1.55;
  }

  .result-score {
    position: relative;
    margin-top: 18px;
    padding: 15px 17px;
    border: 2px solid #ae7138;
    border-radius: 18px;
    background: rgba(255, 249, 216, 0.66);
    box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.55);
  }

  .result-score span,
  .result-stat span {
    display: block;
    color: rgba(77, 42, 20, 0.58);
    font-size: clamp(8px, 2.3cqw, 10px);
    font-weight: 850;
    letter-spacing: 0.12em;
    text-transform: uppercase;
  }

  .result-score strong {
    display: block;
    margin-top: 6px;
    color: #d95a1e;
    font-size: clamp(38px, 12cqw, 62px);
    font-weight: 1000;
    letter-spacing: -0.055em;
    line-height: 1;
    text-shadow: 0 2px 0 #fff0a3;
  }

  .result-stats {
    position: relative;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 7px;
    margin-top: 8px;
  }

  .result-stat {
    min-width: 0;
    padding: 11px 9px;
    border: 2px solid rgba(125, 69, 31, 0.28);
    border-radius: 13px;
    background: rgba(255, 249, 218, 0.5);
  }

  .result-stat strong {
    display: block;
    margin-top: 5px;
    overflow: hidden;
    font-size: clamp(13px, 4cqw, 19px);
    font-weight: 950;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .button-row {
    position: relative;
    display: grid;
    grid-template-columns: 1.2fr 0.8fr;
    gap: 8px;
    margin-top: 14px;
  }

  .button-row .primary-button {
    margin-top: 0;
  }

  body.phase-home #home-screen,
  body.phase-result #result-screen {
    opacity: 1;
    pointer-events: auto;
  }

  body.phase-home #hud-layer,
  body.phase-home #combo-pill,
  body.phase-home #game-hint,
  body.phase-result #hud-layer,
  body.phase-result #combo-pill,
  body.phase-result #game-hint {
    opacity: 0;
  }

  .announcement-kicker,
  .announcement-title {
    color: #ffe98c;
    text-shadow: 0 3px 0 #7a351c;
  }

  .best-line,
  .best-line b {
    color: #79451f;
  }

  .result-icon img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 5px 4px rgba(75, 35, 16, 0.25));
  }

  .entry-loader {
    position: fixed;
    z-index: 100;
    inset: 0;
    width: var(--app-viewport-width);
    height: var(--app-viewport-height);
    overflow: hidden;
    background: transparent;
    opacity: 1;
    visibility: visible;
    transition: opacity 320ms ease, visibility 320ms ease;
  }

  .entry-loader.is-hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
  }

  .loader-board {
    position: absolute;
    left: 0;
    top: 0;
    width: 720px;
    height: 1280px;
    overflow: hidden;
    background: #087ca9;
    box-shadow: none;
    transform: translate3d(var(--stage-x), var(--stage-y), 0) scale(var(--stage-scale));
    transform-origin: 0 0;
  }

  .loader-cover {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
    user-select: none;
  }

  .loader-ui {
    position: absolute;
    z-index: 3;
    left: 120px;
    right: 120px;
    bottom: 34px;
    height: 175px;
  }

  .loader-logo,
  .home-logo {
    position: absolute;
    z-index: 2;
    top: 18px;
    left: 40px;
    width: 640px;
    height: auto;
    pointer-events: none;
    user-select: none;
  }

  .loader-character,
  .home-character {
    position: absolute;
    z-index: 2;
    top: 515px;
    left: 40px;
    width: 640px;
    height: auto;
    pointer-events: none;
    user-select: none;
  }

  .pirate-loading {
    position: absolute;
    inset: 0;
  }

  .pirate-loading-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .pirate-loading-art {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    filter: drop-shadow(0 10px 8px rgba(0, 0, 0, 0.55));
  }

  .pirate-energy-track {
    position: absolute;
    z-index: 2;
    inset: 0;
  }

  .pirate-energy-fill {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    clip-path: inset(0 100% 0 0 round 999px);
  }

  .pirate-loading-frame {
    position: absolute;
    z-index: 1;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
  }

  #hud-layer {
    inset: 14px 14px auto;
    display: grid;
    grid-template-columns: 240px 220px 190px;
    justify-content: space-between;
    gap: 10px;
    padding: 0;
  }

  .hud-chip.voyage { background-image: url("../assets/ui/arrgh-hud-voyage-v1.webp"); }
  .hud-chip.score { background-image: url("../assets/ui/arrgh-hud-score-v1.webp"); text-align: left; }
  .hud-chip.lives { background-image: url("../assets/ui/arrgh-hud-lives-v1.webp"); }

  .hud-row {
    position: absolute;
    left: 76px;
    top: 20px;
    display: flex;
    align-items: baseline;
    gap: 7px;
  }

  .hud-chip.score .hud-value {
    position: absolute;
    left: 76px;
    top: 22px;
    color: #ffd75e;
  }

  body.phase-playing #game-hint.is-visible {
    opacity: 1;
    transform: translate(-50%, 0) scale(1);
  }

  .home-screen {
    padding: 0;
    overflow: hidden;
    background: url("../assets/lobby/home-background.webp") center / cover no-repeat;
  }

  .home-game-ui {
    position: absolute;
    z-index: 4;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    padding-bottom: 48px;
  }

  .home-start-button {
    position: relative;
    z-index: 1;
    width: 320px;
    aspect-ratio: 417 / 174;
    border: 0;
    outline: none;
    background: url("../assets/lobby/home-start-button.png") center / contain no-repeat;
    filter: drop-shadow(0 12px 7px rgba(0, 0, 0, 0.5));
    cursor: pointer;
    transition: transform 120ms ease, filter 120ms ease;
  }

  .home-start-button:focus-visible {
    filter: drop-shadow(0 0 8px rgba(255, 224, 117, 0.82)) drop-shadow(0 12px 7px rgba(0, 0, 0, 0.5)) brightness(1.08);
  }

  .home-start-button:active,
  .home-start-button.is-pressed {
    transform: translateY(4px) scale(0.985);
    filter: drop-shadow(0 7px 5px rgba(0, 0, 0, 0.45)) brightness(0.96);
  }

  .home-game-ui .best-line {
    margin-top: 2px;
    color: rgba(255, 245, 208, 0.8);
    font-size: 12px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.75);
  }

  .home-game-ui .best-line b { color: #ffd75e; }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      scroll-behavior: auto !important;
      transition-duration: 0.01ms !important;
    }
  }
