:root {
  --bg-sky-top: #dff3ff;
  --bg-sky-bottom: #fdf5dc;
  --bg-overlay: rgba(255, 255, 255, 0.65);

  --ink: rgba(19, 27, 42, 0.92);
  --muted: rgba(19, 27, 42, 0.62);
  --muted2: rgba(19, 27, 42, 0.48);

  --panel: rgba(255, 253, 245, 0.82);
  --panel-strong: rgba(255, 253, 245, 0.92);
  --panel-edge: rgba(19, 27, 42, 0.14);
  --panel-edge-strong: rgba(19, 27, 42, 0.22);

  --accent: #2b6cb0;
  --accent2: #0f766e;
  --gold: #b8871b;
  --danger: #b91c1c;

  --shadow: 0 18px 50px rgba(9, 19, 36, 0.18);
  --shadow-soft: 0 10px 28px rgba(9, 19, 36, 0.12);

  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 12px;

  --display: "Fraunces", ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;
  --sans: "Spline Sans", ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--sans);
  -webkit-text-size-adjust: 100%;
  background:
    radial-gradient(900px 600px at 20% 15%, rgba(43, 108, 176, 0.16), transparent 60%),
    radial-gradient(1100px 700px at 80% 20%, rgba(15, 118, 110, 0.12), transparent 58%),
    radial-gradient(1000px 700px at 50% 90%, rgba(184, 135, 27, 0.14), transparent 60%),
    linear-gradient(180deg, var(--bg-sky-top), var(--bg-sky-bottom));
}

/* Helps reduce accidental highlight / “browser-y” feeling on mobile controls. */
* {
  -webkit-tap-highlight-color: transparent;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(45deg, rgba(0, 0, 0, 0.018), rgba(0, 0, 0, 0.018) 2px, transparent 2px, transparent 6px);
  opacity: 0.55;
}

#app {
  max-width: 1440px;
  margin: 0 auto;
}

#scrollShim {
  height: 0px;
}

/* iPad/iPhone landscape Safari: if the page has no scroll, the tab/address chrome won't collapse.
   Provide a small scroll area on coarse pointer devices (touch) even when the layout is "desktop". */
@media (pointer: coarse) and (hover: none) and (orientation: landscape) and (max-width: 1400px) {
  body {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    min-height: calc(var(--ct-vh, 1vh) * 100);
  }

  #scrollShim {
    height: 88px;
  }
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 18px 10px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0.55));
  border: 1px solid var(--panel-edge);
  box-shadow: var(--shadow-soft);
  color: rgba(19, 27, 42, 0.85);
  font-family: var(--mono);
  font-weight: 800;
  letter-spacing: 0.5px;
}

.title .name {
  font-family: var(--display);
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.2px;
}

.title .tag {
  font-size: 12px;
  color: var(--muted);
}

.status {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  padding: 8px 10px;
  border-radius: 999px;
  border: 1px solid rgba(19, 27, 42, 0.14);
  background: rgba(255, 255, 255, 0.55);
}

main {
  display: grid;
  grid-template-columns: 1fr 440px;
  gap: 14px;
  align-items: start;
  padding: 8px 18px 18px;
}

.stage {
  position: relative;
  border-radius: var(--radius-lg);
  padding: 12px;
  background: rgba(255, 255, 255, 0.25);
  border: 1px solid rgba(19, 27, 42, 0.12);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(900px 500px at 30% 10%, rgba(255, 255, 255, 0.48), transparent 60%),
    radial-gradient(900px 700px at 80% 75%, rgba(255, 255, 255, 0.34), transparent 60%);
}

#game {
  width: 100%;
  height: auto;
  display: block;
  position: relative;
  z-index: 1;
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(230, 247, 255, 0.85), rgba(253, 245, 220, 0.65));
  border: 1px solid rgba(19, 27, 42, 0.22);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.9) inset,
    0 14px 32px rgba(9, 19, 36, 0.12);
}

.minimap {
  position: absolute;
  right: 18px;
  bottom: 18px;
  width: 180px;
  height: 120px;
  border-radius: 16px;
  border: 1px solid rgba(19, 27, 42, 0.18);
  background: rgba(255, 255, 255, 0.55);
  box-shadow: 0 14px 30px rgba(9, 19, 36, 0.14);
  z-index: 5;
  pointer-events: none;
}

/* Touch devices (iPhone/iPad): keep the map clean; minimap comes back when the world is large enough. */
@media (pointer: coarse) and (hover: none) {
  .minimap {
    display: none;
  }
}

.hud {
  position: absolute;
  left: 18px;
  right: 18px;
  top: 18px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
  z-index: 6;
}

.hud-left {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.hud-right {
  max-width: 520px;
}

.hud-chip {
  font-family: var(--mono);
  font-size: 11px;
  padding: 8px 10px;
  border-radius: 999px;
  border: 1px solid rgba(19, 27, 42, 0.18);
  background: rgba(255, 255, 255, 0.72);
  box-shadow: 0 10px 20px rgba(9, 19, 36, 0.1);
  color: rgba(19, 27, 42, 0.85);
  appearance: none;
}

.hud-chip-btn {
  pointer-events: auto;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.hud-chip-btn:active {
  transform: translateY(1px);
}

.hud-chip-subtle {
  border-style: dashed;
  background: rgba(255, 255, 255, 0.6);
  color: rgba(19, 27, 42, 0.72);
}

.hud-help {
  font-size: 12px;
  color: rgba(19, 27, 42, 0.7);
  background: rgba(255, 255, 255, 0.62);
  border: 1px solid rgba(19, 27, 42, 0.16);
  border-radius: 999px;
  padding: 8px 12px;
  box-shadow: 0 10px 20px rgba(9, 19, 36, 0.08);
}

.actionbar {
  position: absolute;
  left: 18px;
  bottom: 18px;
  display: flex;
  gap: 10px;
  pointer-events: auto;
  z-index: 6;
}

/* Joystick is a mobile-only control (enabled in mobile media queries). */
.joystick {
  display: none;
}

/* Mobile menu button (enabled in mobile media queries). */
.mobile-fab {
  display: none;
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 90;
  pointer-events: auto;
  width: 42px;
  height: 42px;
  border-radius: 16px;
  border: 1px solid rgba(19, 27, 42, 0.18);
  background: rgba(255, 255, 255, 0.78);
  box-shadow: 0 12px 22px rgba(9, 19, 36, 0.16);
  font-family: var(--mono);
  font-weight: 900;
  color: rgba(19, 27, 42, 0.75);
  place-items: center;
}

.actionbar::before {
  content: "";
  position: absolute;
  inset: -10px -10px -10px -10px;
  border-radius: 18px;
  background:
    radial-gradient(120px 80px at 20% 20%, rgba(255, 255, 255, 0.38), transparent 65%),
    linear-gradient(180deg, rgba(184, 135, 27, 0.18), rgba(184, 135, 27, 0.06));
  border: 1px solid rgba(19, 27, 42, 0.18);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.75) inset,
    0 14px 30px rgba(9, 19, 36, 0.14);
}

.slot {
  width: 120px;
  height: 52px;
  border-radius: 14px;
  border: 1px solid rgba(19, 27, 42, 0.2);
  background:
    radial-gradient(80px 50px at 20% 30%, rgba(255, 255, 255, 0.65), transparent 65%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(255, 255, 255, 0.62));
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.85) inset,
    0 12px 26px rgba(9, 19, 36, 0.14);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px;
  gap: 8px;
  cursor: pointer;
  position: relative;
  z-index: 1;
}

/* iOS Safari: hard-disable gesture zoom on hotbar buttons (double-tap zoom can still sneak through). */
@media (pointer: coarse) and (hover: none) {
  .slot {
    touch-action: none;
  }
}

.slot:hover {
  transform: translateY(-1px);
}

.slot:active {
  transform: translateY(0px);
}

.slot.is-pressed {
  transform: translateY(0px);
  border-color: rgba(43, 108, 176, 0.34);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.85) inset,
    0 12px 26px rgba(9, 19, 36, 0.14),
    0 0 0 3px rgba(43, 108, 176, 0.12);
}

.slot[data-spell]::after {
  content: "";
  position: absolute;
  right: 10px;
  top: 8px;
  padding: 4px 6px;
  border-radius: 999px;
  border: 1px solid rgba(19, 27, 42, 0.14);
  background: rgba(255, 255, 255, 0.72);
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.6px;
  color: rgba(19, 27, 42, 0.62);
}

.slot[data-spell="arrow"]::after { content: "ARW"; }
.slot[data-spell="fireball"]::after { content: "FIR"; }
.slot[data-spell="hail"]::after { content: "ICE"; }
.slot[data-spell="cleave"]::after { content: "CLV"; }
.slot[data-spell="flurry"]::after { content: "FLR"; }
.slot[data-spell="signature"]::after { content: "ATK"; }

.slot-ghost {
  opacity: 0.45;
  cursor: default;
}

.slot-key {
  width: 26px;
  height: 26px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: rgba(184, 135, 27, 0.14);
  border: 1px solid rgba(184, 135, 27, 0.25);
  font-family: var(--mono);
  font-weight: 900;
  color: rgba(184, 135, 27, 0.95);
}

.slot-name {
  flex: 1;
  text-align: left;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.2px;
  color: rgba(19, 27, 42, 0.82);
  overflow: hidden;
  white-space: normal;
  line-height: 1.05;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.panel {
  display: flex;
  flex-direction: column;
}

.ui-window {
  position: relative;
  border-radius: var(--radius-lg);
  border: 1px solid rgba(19, 27, 42, 0.18);
  background: rgba(255, 255, 255, 0.38);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.ui-window::before {
  content: "";
  position: absolute;
  pointer-events: none;
  inset: 0;
  border-radius: var(--radius-lg);
}

.ui-window-top {
  padding: 10px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.78), rgba(255, 255, 255, 0.5));
  border-bottom: 1px solid rgba(19, 27, 42, 0.12);
}

.ui-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.ui-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  flex: 1 1 auto;
}

.ui-tab {
  border: 1px solid rgba(19, 27, 42, 0.14);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(255, 255, 255, 0.62));
  color: rgba(19, 27, 42, 0.78);
  border-radius: 999px;
  padding: 8px 12px;
  font-weight: 800;
  letter-spacing: 0.2px;
  cursor: pointer;
  font-size: 12px;
}

.ui-tab:hover {
  background: rgba(255, 255, 255, 0.85);
}

.ui-tab.is-active {
  border-color: rgba(43, 108, 176, 0.35);
  background: rgba(43, 108, 176, 0.12);
  color: rgba(43, 108, 176, 0.95);
}

.ui-tab.is-advanced {
  display: none;
}

.ui-more {
  position: relative;
  display: flex;
  align-items: center;
}

.ui-more-btn {
  border: 1px solid rgba(19, 27, 42, 0.14);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(255, 255, 255, 0.62));
  color: rgba(19, 27, 42, 0.78);
  border-radius: 999px;
  padding: 8px 12px;
  font-weight: 900;
  letter-spacing: 0.2px;
  cursor: pointer;
  font-size: 12px;
  min-width: 44px;
}

.ui-more-btn:hover {
  background: rgba(255, 255, 255, 0.85);
}

.ui-more-btn.is-active {
  border-color: rgba(43, 108, 176, 0.35);
  background: rgba(43, 108, 176, 0.12);
  color: rgba(43, 108, 176, 0.95);
}

.ui-more-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  min-width: 160px;
  border-radius: 14px;
  border: 1px solid rgba(19, 27, 42, 0.16);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 18px 42px rgba(9, 19, 36, 0.16);
  padding: 8px;
  z-index: 120;
  backdrop-filter: blur(10px);
}

.ui-more-item {
  width: 100%;
  text-align: left;
  border: 1px solid rgba(19, 27, 42, 0.12);
  background: rgba(255, 255, 255, 0.7);
  color: rgba(19, 27, 42, 0.82);
  border-radius: 12px;
  padding: 10px 12px;
  font-weight: 900;
  letter-spacing: 0.2px;
  cursor: pointer;
  font-size: 12px;
}

.ui-more-item + .ui-more-item {
  margin-top: 8px;
}

.ui-more-item:hover {
  background: rgba(43, 108, 176, 0.12);
  border-color: rgba(43, 108, 176, 0.28);
  color: rgba(43, 108, 176, 0.95);
}

.ui-window-body {
  padding: 12px;
  background: var(--panel);
}

.tab {
  display: none;
}

.tab.is-active {
  display: block;
}

.card-title {
  font-family: var(--display);
  font-size: 15px;
  font-weight: 850;
  letter-spacing: 0.25px;
  margin-bottom: 12px;
}

.row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.row-inline {
  display: flex;
  gap: 10px;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
}

.seg-mini .seg-btn {
  padding: 8px 10px;
  font-size: 11px;
}

.kpi-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pill-kpi {
  padding: 6px 10px;
  font-size: 11px;
}

@media (min-width: 1101px) {
  /* Desktop: keep default page scroll so the right panel can grow naturally. */
  body {
    overflow: auto;
  }
}

/* Mobile-first UX: keep the map big, move panels into a bottom drawer. */
@media (max-width: 640px), (pointer: coarse) and (hover: none) and (orientation: portrait) {
  :root {
    --drawer-collapsed: 86px;
    --drawer-expanded: 62dvh;
    /* Size controls with the device width so Safari/Chrome feel consistent. */
    --ct-joy-size: clamp(146px, 26vw, 188px);
    --ct-action-gap: 10px;
  }

  body {
    /* Allow a tiny amount of scroll so iOS Safari can hide its URL/tab chrome. */
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    min-height: calc(var(--ct-vh, 1vh) * 100);
  }

  #app {
    max-width: none;
    /* Keep the game pinned while the page scrolls a few px to let iOS Safari collapse its chrome. */
    position: fixed;
    inset: 0;
    margin: 0;
  }

  header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 80;
    padding: 10px 12px 8px;
    backdrop-filter: blur(10px);
  }

  .title .tag {
    display: none;
  }

  .logo {
    width: 38px;
    height: 38px;
    border-radius: 13px;
  }

  .title .name {
    font-size: 17px;
  }

  .status {
    padding: 7px 9px;
    font-size: 11px;
  }

  main {
    display: block;
    padding: 0;
    padding-top: var(--ct-header-h, 64px);
    height: calc(var(--ct-vh, 1vh) * 100 - var(--ct-header-h, 64px));
    overflow: hidden;
  }

  .stage {
    padding: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
    background: transparent;
    overflow: hidden;
    /* give the fixed controls a safe “resting” layer */
    padding-bottom: calc(var(--drawer-collapsed) + 148px + env(safe-area-inset-bottom));
    height: 100%;
  }

  .minimap {
    /* On phones the main map is already small. Hide minimap by default to reduce clutter.
       (We can re-enable later for large-world navigation.) */
    display: none;
  }

  #game {
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }

  /* HUD: keep it minimal on phones. */
  .hud {
    left: 10px;
    right: 10px;
    top: 10px;
    gap: 8px;
  }

  .hud-chip {
    font-size: 10px;
    padding: 6px 8px;
  }

  .hud-help {
    display: none;
  }

  /* Reduce clutter: show only name + level by default. */
  #hudJob,
  #hudMode {
    display: none;
  }

  /* Mobile portrait: keep the map clean — controls sit near the bottom edge, not on the right half. */
  .joystick {
    display: block;
    width: var(--ct-joy-size);
    height: var(--ct-joy-size);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    position: fixed;
    left: 12px;
    bottom: calc(var(--drawer-collapsed) + 12px + env(safe-area-inset-bottom));
    z-index: 70;
    pointer-events: auto;
  }

  .joystick-base {
    position: absolute;
    inset: 0;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.46);
    border: 1px solid rgba(19, 27, 42, 0.18);
    box-shadow: 0 14px 26px rgba(9, 19, 36, 0.14);
    backdrop-filter: blur(6px);
  }

.joystick-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 60px;
    height: 60px;
    margin-left: -30px;
    margin-top: -30px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.82);
    border: 1px solid rgba(19, 27, 42, 0.18);
    box-shadow:
      0 1px 0 rgba(255, 255, 255, 0.85) inset,
      0 14px 26px rgba(9, 19, 36, 0.14);
    transform: translate3d(0, 0, 0);
    transition: transform 80ms ease;
}

.joystick.is-active .joystick-knob {
  transition: none;
}

.joystick.is-disabled {
  opacity: 0.62;
  filter: saturate(0.95);
}

  .actionbar {
    /* Mobile portrait: classic ARPG layout — left thumb moves, right thumb attacks. */
    position: fixed;
    left: auto;
    transform: none;
    right: calc(12px + env(safe-area-inset-right));
    bottom: calc(var(--drawer-collapsed) + 12px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: 58px;
    gap: var(--ct-action-gap);
    /* Avoid overlapping the left joystick: reserve joy size + margins. */
    width: clamp(
      220px,
      calc(100vw - var(--ct-joy-size) - 36px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      320px
    );
    margin: 0;
    z-index: 70;
  }

  /* If the panel is expanded, de-emphasize controls to avoid overlap. */
  body.ct-panel-open .actionbar,
  body.ct-panel-open .joystick {
    opacity: 0.18;
    pointer-events: none;
  }

  .actionbar::before {
    display: none;
  }

  .slot {
    width: auto;
    height: 58px;
    padding: 10px 10px;
    border-radius: 18px;
    /* iOS Safari: prevent double-tap zoom on the action bar. */
    touch-action: none;
    min-width: 0;
  }

  .slot-ghost {
    display: none;
  }

  .slot-key {
    width: 30px;
    height: 30px;
    border-radius: 10px;
  }

  .slot-name {
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    min-width: 0;
    overflow: hidden;
  }

  .slot[data-spell]::after {
    right: 8px;
    top: 8px;
    padding: 3px 6px;
    font-size: 10px;
  }

  /* Mobile menu button on the map (open the drawer). */
  .mobile-fab {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 90;
    pointer-events: auto;
    width: 42px;
    height: 42px;
    border-radius: 16px;
    border: 1px solid rgba(19, 27, 42, 0.18);
    background: rgba(255, 255, 255, 0.78);
    box-shadow: 0 12px 22px rgba(9, 19, 36, 0.16);
    font-family: var(--mono);
    font-weight: 900;
    color: rgba(19, 27, 42, 0.75);
    display: grid;
    place-items: center;
    touch-action: manipulation;
  }

  /* Bottom drawer. */
  .panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    height: min(var(--drawer-expanded), 560px);
    padding: 0;
  }

  .panel .ui-window {
    height: 100%;
    border-radius: 18px 18px 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
  }

  .drawer-handle {
    width: 46px;
    height: 6px;
    border-radius: 999px;
    border: 0;
    padding: 0;
    margin: 2px auto 8px;
    display: block;
    background: rgba(19, 27, 42, 0.18);
    cursor: pointer;
  }

  .ui-topbar {
    gap: 8px;
  }

  .ui-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }

  .ui-tabs::-webkit-scrollbar {
    display: none;
  }

  .ui-tab {
    white-space: nowrap;
    padding: 8px 10px;
    font-size: 12px;
  }

  .ui-window-body {
    height: calc(100% - 66px);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }

  /* Collapsed: show only the top bar (tabs). */
  .panel.is-collapsed {
    height: calc(var(--drawer-collapsed) + env(safe-area-inset-bottom));
  }

  .panel.is-collapsed .ui-window-body {
    display: none;
  }

  /* Make the whole map area ignore browser zoom gestures; interactions are game-controlled. */
  #game {
    /* Allow vertical panning so iOS Safari can collapse its URL/tab chrome.
       We don't rely on drag gestures on the map; taps are enough for click-to-move. */
    touch-action: pan-y;
  }

  .ui-tab,
  .seg-btn,
  .btn,
  input,
  textarea,
  select {
    touch-action: manipulation;
  }

  /* A tiny scroll target to let Safari hide its top/bottom chrome. */
  #scrollShim {
    /* #app is position:fixed in mobile mode, so the shim must include one viewport height + a small extra. */
    height: calc(var(--ct-vh, 1vh) * 100 + 120px);
  }
}

/* Touch landscape (phones/tablets): map-first, panels slide from the right. */
@media (pointer: coarse) and (hover: none) and (orientation: landscape) and (max-width: 1400px) {
  :root {
    --ct-joy-size-land: clamp(120px, 22vw, 160px);
    --ct-action-gap-land: 10px;
  }
  body {
    /* Keep a (tiny) scrollable page so iOS Safari can collapse its chrome. */
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    min-height: calc(var(--ct-vh, 1vh) * 100);
  }

  #app {
    /* Keep the game pinned while the page scrolls a few px to let iOS Safari collapse its chrome. */
    position: fixed;
    inset: 0;
    margin: 0;
    max-width: none;
  }

  header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 80;
  }

  main {
    display: block;
    padding: 0;
    padding-top: var(--ct-header-h, 64px);
    height: calc(var(--ct-vh, 1vh) * 100 - var(--ct-header-h, 64px));
    overflow: hidden;
  }

  .stage {
    padding: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
    background: transparent;
    overflow: hidden;
    height: 100%;
  }

  #game {
    border-radius: 0;
    border: 0;
    box-shadow: none;
    /* Allow vertical panning so iOS Safari can collapse its chrome even when the map is full-screen. */
    touch-action: pan-y;
  }

  .minimap {
    /* Hide by default on phones; keeps the map clear. */
    display: none;
  }

  /* Make the menu button available even when width > 640. */
  .mobile-fab {
    display: grid;
  }

  /* Bring back mobile controls (joystick + action bar) in landscape.
     iPhone landscape is wider than 640px so it doesn't hit the portrait mobile query. */
  .joystick {
    display: block;
    width: var(--ct-joy-size-land);
    height: var(--ct-joy-size-land);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    position: fixed;
    left: calc(12px + env(safe-area-inset-left));
    bottom: calc(12px + env(safe-area-inset-bottom));
    z-index: 70;
    pointer-events: auto;
  }

  .actionbar {
    position: fixed;
    left: auto;
    transform: none;
    right: calc(12px + env(safe-area-inset-right));
    bottom: calc(12px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: 54px;
    gap: var(--ct-action-gap-land);
    width: clamp(
      220px,
      calc(100vw - var(--ct-joy-size-land) - 36px - env(safe-area-inset-left) - env(safe-area-inset-right)),
      320px
    );
    margin: 0;
    z-index: 70;
  }

  .actionbar::before {
    display: none;
  }

  .slot {
    width: auto;
    height: 54px;
    padding: 9px 10px;
    border-radius: 18px;
    touch-action: none;
    min-width: 0;
  }

  .slot-ghost {
    display: none;
  }

  /* Panel becomes a right drawer (hidden when collapsed). */
  .panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: auto;
    z-index: 60;
    width: min(420px, 52vw);
    height: auto;
    padding: 0;
    transform: translate3d(0, 0, 0);
    transition: transform 180ms ease;
  }

  .panel.is-collapsed {
    transform: translate3d(110%, 0, 0);
  }

  .panel .ui-window {
    height: 100%;
    border-radius: 18px 0 0 18px;
    border-top: 0;
    border-right: 0;
    border-bottom: 0;
  }

  .drawer-handle {
    display: none;
  }

  /* In landscape, Safari sometimes needs a larger scroll target to hide tabs/address bar. */
  #scrollShim {
    /* #app is position:fixed in mobile mode, so the shim must include one viewport height + a small extra. */
    height: calc(var(--ct-vh, 1vh) * 100 + 140px);
  }
}

label {
  font-size: 12px;
  color: var(--muted);
}

.helper {
  font-size: 12px;
  color: var(--muted2);
  line-height: 1.35;
}

.avatar-row label {
  margin-bottom: 8px;
}

.avatar-picker {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.avatar-preview {
  width: 84px;
  height: 84px;
  border-radius: 18px;
  border: 1px solid rgba(19, 27, 42, 0.16);
  background: rgba(255, 255, 255, 0.68);
  box-shadow: 0 10px 24px rgba(9, 19, 36, 0.12);
  object-fit: cover;
}

.avatar-actions {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

.avatar-bg-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.72);
  user-select: none;
}

.avatar-bg-toggle input {
  width: 16px;
  height: 16px;
}

.avatar-actions .helper {
  margin-top: 0;
  align-self: stretch;
}

@media (max-width: 640px) {
  .avatar-preview {
    width: 64px;
    height: 64px;
    border-radius: 16px;
  }
}

input,
textarea,
select {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid rgba(19, 27, 42, 0.18);
  background: rgba(255, 255, 255, 0.82);
  border-radius: var(--radius-sm);
  padding: 10px 10px;
  color: rgba(19, 27, 42, 0.9);
  font-family: var(--sans);
  font-size: 13px;
  outline: none;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.85) inset;
}

input:focus,
textarea:focus,
select:focus {
  border-color: rgba(43, 108, 176, 0.5);
  box-shadow:
    0 0 0 3px rgba(43, 108, 176, 0.14),
    0 1px 0 rgba(255, 255, 255, 0.85) inset;
}

textarea {
  resize: vertical;
}

/* Desktop default: hide mobile-only affordances. */
@media (min-width: 641px) and (min-height: 521px) and (hover: hover) and (pointer: fine) {
  .mobile-fab,
  .drawer-handle {
    display: none;
  }
}

.btn {
  border: 1px solid rgba(43, 108, 176, 0.28);
  background: linear-gradient(180deg, rgba(43, 108, 176, 0.14), rgba(43, 108, 176, 0.08));
  color: rgba(43, 108, 176, 0.95);
  padding: 10px 12px;
  border-radius: 999px;
  font-weight: 900;
  cursor: pointer;
  font-size: 12px;
  letter-spacing: 0.2px;
  box-shadow: 0 10px 22px rgba(9, 19, 36, 0.1);
}

.btn:hover {
  background: linear-gradient(180deg, rgba(43, 108, 176, 0.18), rgba(43, 108, 176, 0.1));
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.48;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-mini {
  padding: 7px 10px;
  font-size: 11px;
  letter-spacing: 0.1px;
  box-shadow: none;
}

.btn-ghost {
  border: 1px solid rgba(19, 27, 42, 0.16);
  background: rgba(255, 255, 255, 0.55);
  color: rgba(19, 27, 42, 0.78);
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.76);
}

.btn.is-active {
  border-color: rgba(43, 108, 176, 0.35);
  background: rgba(43, 108, 176, 0.12);
  color: rgba(43, 108, 176, 0.95);
}

.seg {
  display: flex;
  border: 1px solid rgba(19, 27, 42, 0.16);
  border-radius: 999px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.6);
}

.seg-btn {
  flex: 1;
  padding: 10px 10px;
  border: none;
  background: transparent;
  color: rgba(19, 27, 42, 0.72);
  cursor: pointer;
  font-weight: 900;
  font-size: 12px;
}

.seg-btn.is-active {
  background: rgba(15, 118, 110, 0.12);
  color: rgba(15, 118, 110, 0.95);
}

.chat-filter {
  width: 100%;
  max-width: 360px;
}

.tab-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  margin-left: 8px;
  border-radius: 999px;
  background: rgba(251, 191, 36, 0.92);
  color: rgba(19, 27, 42, 0.95);
  border: 1px solid rgba(19, 27, 42, 0.18);
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1;
  box-shadow: 0 10px 22px rgba(9, 19, 36, 0.14);
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid rgba(19, 27, 42, 0.16);
  background: rgba(255, 255, 255, 0.65);
  font-family: var(--mono);
  font-size: 12px;
  color: rgba(19, 27, 42, 0.78);
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

.stat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 16px;
  border: 1px solid rgba(19, 27, 42, 0.14);
  background: rgba(255, 255, 255, 0.62);
}

.stat-key {
  font-family: var(--mono);
  font-weight: 900;
  letter-spacing: 1px;
  color: rgba(19, 27, 42, 0.78);
}

.stat-val {
  font-family: var(--mono);
  font-weight: 900;
  color: rgba(19, 27, 42, 0.9);
  min-width: 26px;
  text-align: right;
}

@media (max-width: 720px) {
  .stat-grid {
    grid-template-columns: 1fr;
  }
}

.feed {
  height: 240px;
  overflow: auto;
  border: 1px solid rgba(19, 27, 42, 0.14);
  border-radius: 16px;
  padding: 10px;
  background: rgba(255, 255, 255, 0.62);
  font-size: 12px;
}

.item {
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px dashed rgba(19, 27, 42, 0.12);
}

.item:last-child {
  margin-bottom: 0;
  padding-bottom: 0;
  border-bottom: none;
}

.meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: rgba(19, 27, 42, 0.54);
  font-family: var(--mono);
  font-size: 11px;
}

.content {
  margin-top: 6px;
  color: rgba(19, 27, 42, 0.86);
  line-height: 1.35;
}

.code {
  font-family: var(--mono);
  font-weight: 900;
  letter-spacing: 2px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid rgba(184, 135, 27, 0.28);
  background: rgba(184, 135, 27, 0.08);
  color: rgba(184, 135, 27, 0.95);
  min-width: 112px;
  text-align: center;
}

.divider {
  height: 1px;
  background: rgba(19, 27, 42, 0.12);
  margin: 12px 0;
}

.hat-line {
  color: rgba(19, 27, 42, 0.78);
  font-size: 12px;
  line-height: 1.35;
  margin-bottom: 12px;
}

.hat-scene {
  display: grid;
  grid-template-columns: 150px 1fr;
  gap: 12px;
  align-items: start;
}

.hat-portrait {
  border-radius: 16px;
  border: 1px solid rgba(19, 27, 42, 0.16);
  background:
    radial-gradient(140px 110px at 50% 0%, rgba(184, 135, 27, 0.18), transparent 70%),
    linear-gradient(180deg, rgba(255, 255, 255, 0.86), rgba(255, 255, 255, 0.66));
  box-shadow: 0 12px 28px rgba(9, 19, 36, 0.12);
  padding: 10px;
}

.hat-portrait-cap {
  font-family: var(--mono);
  font-weight: 900;
  font-size: 12px;
  letter-spacing: 1px;
  color: rgba(19, 27, 42, 0.75);
}

.hat-portrait-sub {
  margin-top: 8px;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.62);
  line-height: 1.35;
}

.hat-dialogue {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.hat-log {
  border-radius: 16px;
  border: 1px solid rgba(19, 27, 42, 0.14);
  background: rgba(255, 255, 255, 0.6);
  padding: 10px;
  max-height: 190px;
  overflow: auto;
}

.hat-turn {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.hat-turn:last-child {
  margin-bottom: 0;
}

.hat-who {
  font-family: var(--mono);
  font-size: 11px;
  color: rgba(19, 27, 42, 0.6);
  min-width: 52px;
}

.hat-said {
  color: rgba(19, 27, 42, 0.84);
  line-height: 1.4;
  font-size: 12px;
}

.hat-options {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.hat-choice {
  border: 1px solid rgba(19, 27, 42, 0.16);
  background: rgba(255, 255, 255, 0.7);
  border-radius: 999px;
  padding: 9px 12px;
  cursor: pointer;
  font-weight: 900;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.78);
}

.hat-choice:hover {
  background: rgba(255, 255, 255, 0.88);
  border-color: rgba(184, 135, 27, 0.28);
}

.hat-free {
  display: none;
  gap: 8px;
  align-items: center;
}

.hat-free.is-active {
  display: flex;
}

.hat-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.hat-result {
  border: 1px solid rgba(15, 118, 110, 0.2);
  background: rgba(15, 118, 110, 0.07);
  border-radius: 16px;
  padding: 12px;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.86);
}

@media (max-width: 520px) {
  .hat-scene {
    grid-template-columns: 1fr;
  }
}

.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(9, 19, 36, 0.28);
  backdrop-filter: blur(10px);
  z-index: 30;
}

.modal.is-open {
  display: flex;
}

.modal-card {
  width: min(560px, 100%);
  border-radius: 22px;
  background: rgba(255, 253, 245, 0.94);
  border: 1px solid rgba(19, 27, 42, 0.18);
  box-shadow: 0 30px 90px rgba(9, 19, 36, 0.35);
  padding: 16px;
}

.modal-title {
  font-family: var(--display);
  font-size: 20px;
  font-weight: 900;
  margin-bottom: 8px;
}

.modal-text {
  color: rgba(19, 27, 42, 0.76);
  line-height: 1.45;
  font-size: 13px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.modal-foot {
  margin-top: 12px;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.56);
}

@media (max-width: 1100px) {
  main {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 1101px) and (hover: hover) and (pointer: fine) {
  /* Desktop: right panel scroll only; left stage stays fixed. */
  body {
    overflow: hidden;
  }

  #app {
    height: 100vh;
    display: flex;
    flex-direction: column;
  }

  main {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    grid-template-rows: 1fr;
  }

  .stage,
  .panel {
    min-height: 0;
  }

  .stage {
    height: 100%;
    overflow: hidden;
  }

  .panel {
    height: 100%;
    overflow: hidden;
  }

  .ui-window {
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .ui-window-body {
    flex: 1;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
  }
}

/* Inventory / Equipment */

.equip-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.equip-slot {
  border: 1px solid rgba(19, 27, 42, 0.12);
  background: rgba(255, 255, 255, 0.72);
  border-radius: 12px;
  padding: 10px 12px;
}

.equip-label {
  font-size: 12px;
  color: rgba(19, 27, 42, 0.6);
}

.equip-value {
  margin-top: 4px;
  font-size: 13px;
  color: rgba(19, 27, 42, 0.9);
  font-family: var(--mono);
}

.inventory {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 10px;
}

.inv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid rgba(19, 27, 42, 0.12);
  background: rgba(255, 255, 255, 0.72);
  border-radius: 12px;
  padding: 10px 12px;
}

.inv-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.inv-name {
  font-size: 13px;
  color: rgba(19, 27, 42, 0.92);
}

.inv-sub {
  font-size: 12px;
  color: rgba(19, 27, 42, 0.6);
  font-family: var(--mono);
}

.inv-actions {
  display: flex;
  gap: 8px;
}

.achievements {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 10px;
}

.ach {
  border: 1px solid rgba(19, 27, 42, 0.12);
  background: rgba(255, 255, 255, 0.72);
  border-radius: 12px;
  padding: 10px 12px;
}

.ach-title {
  font-size: 13px;
  color: rgba(19, 27, 42, 0.92);
}

.ach-sub {
  margin-top: 2px;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.6);
}

/* --- Tooltips (hover help) --- */
.pill[data-tip],
.stat-key[data-tip] {
  cursor: help;
}

.ct-tooltip {
  position: fixed;
  z-index: 9999;
  max-width: 320px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(15, 20, 30, 0.94);
  color: rgba(255, 255, 255, 0.96);
  font-size: 12px;
  line-height: 1.35;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.25);
  transform: translate3d(0, 0, 0) scale(0.98);
  transform-origin: 12px 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease, transform 120ms ease;
  white-space: pre-line;
}

.ct-tooltip.is-visible {
  opacity: 1;
  transform: translate3d(0, 0, 0) scale(1);
}

/* --- Coach (micro-tutorial) --- */
.ct-coach-bubble {
  position: fixed;
  z-index: 9998;
  max-width: 320px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.92);
  color: rgba(19, 27, 42, 0.92);
  border: 1px solid rgba(19, 27, 42, 0.14);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.16);
  font-size: 13px;
  line-height: 1.35;
  backdrop-filter: blur(10px);
}

.ct-coach-toast-text {
  white-space: pre-line;
}

.ct-coach-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 10px;
}

.ct-coach-btn {
  border: 1px solid rgba(19, 27, 42, 0.16);
  background: rgba(255, 255, 255, 0.65);
  color: rgba(19, 27, 42, 0.86);
  padding: 7px 10px;
  border-radius: 999px;
  font-weight: 900;
  cursor: pointer;
  font-size: 12px;
  letter-spacing: 0.15px;
  touch-action: manipulation;
}

.ct-coach-btn.ct-primary {
  border-color: rgba(43, 108, 176, 0.28);
  background: linear-gradient(180deg, rgba(43, 108, 176, 0.16), rgba(43, 108, 176, 0.08));
  color: rgba(43, 108, 176, 0.95);
}

.ct-coach-btn:active {
  transform: translateY(1px);
}

.ct-coach-highlight {
  outline: 2px solid rgba(255, 156, 69, 0.9);
  box-shadow: 0 0 0 6px rgba(255, 156, 69, 0.22);
  border-radius: 14px;
  animation: ct-coach-pulse 900ms ease-in-out infinite;
}

@keyframes ct-coach-pulse {
  0% {
    box-shadow: 0 0 0 6px rgba(255, 156, 69, 0.18);
  }
  50% {
    box-shadow: 0 0 0 9px rgba(255, 156, 69, 0.28);
  }
  100% {
    box-shadow: 0 0 0 6px rgba(255, 156, 69, 0.18);
  }
}

/* --- Celebration (first kill) --- */
.ct-confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9997;
  overflow: hidden;
}

.ct-confetti-piece {
  position: absolute;
  top: -18px;
  border-radius: 3px;
  opacity: 0.95;
  transform: translate3d(0, 0, 0) rotate(0deg);
  animation-name: ct-confetti-fall;
  animation-timing-function: cubic-bezier(0.15, 0.6, 0.2, 1);
  animation-fill-mode: forwards;
}

@keyframes ct-confetti-fall {
  0% {
    transform: translate3d(0, -10px, 0) rotate(0deg);
    opacity: 0.95;
  }
  100% {
    transform: translate3d(0, 110vh, 0) rotate(680deg);
    opacity: 0;
  }
}

/* --- Details (advanced) --- */
.ui-details {
  margin-top: 10px;
  border: 1px solid rgba(19, 27, 42, 0.12);
  background: rgba(255, 255, 255, 0.68);
  border-radius: 14px;
  padding: 10px 12px;
}

.ui-details-summary {
  cursor: pointer;
  font-size: 12px;
  color: rgba(19, 27, 42, 0.7);
  font-family: var(--mono);
}
