/* CHIDOL design system — shared tokens and components. Pages build on these classes;
   page-specific styles live next to the page module. Dark-only (game UI). */

:root {
  color-scheme: dark;
  --bg: #090b10;
  --surface: #11141c;
  --surface-2: #171b26;
  --surface-3: #202536;
  --overlay: rgba(6, 8, 12, .72);
  --border: rgba(255, 255, 255, .08);
  --border-strong: rgba(255, 255, 255, .16);
  --text: #eef0f6;
  --text-muted: #9aa3b8;
  --text-faint: #6b7489;

  --primary: #6d7cff;
  --primary-hover: #8291ff;
  --primary-2: #a05cff;
  --primary-grad: linear-gradient(135deg, #5b7cff 0%, #a05cff 100%);
  --primary-soft: rgba(109, 124, 255, .14);
  --focus: 0 0 0 3px rgba(109, 124, 255, .45);

  /* PlayStation face-button accents */
  --ps-triangle: #2fd3a0;
  --ps-circle: #ff5c7a;
  --ps-cross: #5b9dff;
  --ps-square: #e07bff;
  --success: #2fd3a0;
  --warning: #ffb547;
  --danger: #ff5c7a;
  --info: #5b9dff;

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-full: 999px;
  --shadow: 0 8px 24px rgba(0, 0, 0, .35);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, .55);
  --glow: 0 0 0 1px rgba(109, 124, 255, .35), 0 8px 32px rgba(109, 124, 255, .25);

  --font: 'Inter', 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-display: 'Press Start 2P', ui-monospace, monospace;
  --font-mono: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;

  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px; --s8: 64px;
  --nav-h: 64px;
  --container: 1200px;
  --ease: cubic-bezier(.2, .8, .2, 1);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scrollbar-color: var(--surface-3) var(--bg); }
* { scrollbar-color: var(--surface-3) transparent; scrollbar-width: thin; }
body {
  margin: 0;
  min-height: 100dvh;
  font: 15px/1.6 var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 0% -10%, rgba(91, 124, 255, .16), transparent 60%),
    radial-gradient(900px 520px at 110% 0%, rgba(160, 92, 255, .12), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; line-height: 1.25; letter-spacing: -.01em; font-weight: 700; }
h1 { font-size: clamp(24px, 3vw, 32px); }
h2 { font-size: 22px; }
h3 { font-size: 17px; }
p { margin: 0; }
a { color: var(--primary-hover); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; display: block; }
:focus-visible { outline: none; box-shadow: var(--focus); }
::selection { background: rgba(109, 124, 255, .4); }

/* Layout */
.container { width: 100%; max-width: var(--container); margin: 0 auto; padding: 0 var(--s5); }
.page { padding-top: var(--s6); padding-bottom: var(--s8); animation: fade-up .35s var(--ease); }
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4); flex-wrap: wrap; margin-bottom: var(--s5); }
.page-header .subtitle { color: var(--text-muted); margin-top: var(--s1); }
.stack { display: flex; flex-direction: column; gap: var(--s4); }
.stack-sm { display: flex; flex-direction: column; gap: var(--s2); }
.row { display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); }
.grid { display: grid; gap: var(--s5); grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.spacer { flex: 1; }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.small { font-size: 13px; }
.mono { font-family: var(--font-mono); }
.center { text-align: center; }
.hidden { display: none !important; }
.divider { height: 1px; background: var(--border); border: 0; margin: var(--s4) 0; }

/* Navbar */
.navbar {
  position: sticky; top: 0; z-index: 50; height: var(--nav-h);
  background: rgba(9, 11, 16, .72); backdrop-filter: saturate(140%) blur(16px);
  border-bottom: 1px solid var(--border);
}
.navbar .container { height: 100%; display: flex; align-items: center; gap: var(--s5); }
.brand { display: flex; align-items: center; gap: var(--s3); color: var(--text); font-family: var(--font-display); font-size: 13px; letter-spacing: .04em; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center;
  background: var(--primary-grad); box-shadow: var(--glow); font-size: 12px; color: #fff;
}
.nav-links { display: flex; gap: var(--s1); }
.nav-link { padding: 8px 14px; border-radius: var(--radius-full); color: var(--text-muted); font-weight: 500; }
.nav-link:hover { color: var(--text); background: var(--surface-2); text-decoration: none; }
.nav-link.active { color: var(--text); background: var(--surface-3); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  height: 40px; padding: 0 18px; border-radius: var(--radius); border: 1px solid var(--border-strong);
  background: var(--surface-2); color: var(--text); font: 600 14px/1 var(--font); cursor: pointer;
  transition: transform .12s var(--ease), background .15s, border-color .15s, box-shadow .15s; white-space: nowrap;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:active { transform: translateY(1px) scale(.99); }
.btn:disabled, .btn[aria-disabled='true'] { opacity: .5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--primary-grad); border-color: transparent; color: #fff; box-shadow: 0 6px 20px rgba(109, 124, 255, .35); }
.btn-primary:hover { background: var(--primary-grad); filter: brightness(1.1); box-shadow: var(--glow); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: rgba(255, 92, 122, .12); border-color: rgba(255, 92, 122, .35); color: #ff8aa0; }
.btn-danger:hover { background: rgba(255, 92, 122, .2); }
.btn-success { background: rgba(47, 211, 160, .14); border-color: rgba(47, 211, 160, .35); color: #5fe6bb; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; border-radius: var(--radius-sm); }
.btn-lg { height: 52px; padding: 0 28px; font-size: 16px; border-radius: var(--radius-lg); }
.btn-block { width: 100%; }
.btn-icon { width: 40px; padding: 0; }
.btn-icon.btn-sm { width: 32px; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 6px; }
.label { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.input, .select, .textarea {
  width: 100%; height: 44px; padding: 0 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border-strong); color: var(--text);
  font: 15px var(--font); transition: border-color .15s, box-shadow .15s;
}
select.select, select.input {
  appearance: none; -moz-appearance: none; padding-right: 38px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%239aa3b8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
select.select option { background: var(--surface-2); color: var(--text); }
.textarea { height: auto; min-height: 96px; padding: 12px 14px; resize: vertical; }
.input::placeholder { color: var(--text-faint); }
.input:hover, .select:hover { border-color: rgba(255, 255, 255, .24); }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--primary); box-shadow: var(--focus); }
.input[aria-invalid='true'] { border-color: var(--danger); }
.hint { font-size: 12px; color: var(--text-faint); }
.field-error { font-size: 12px; color: var(--danger); }
.input-group { position: relative; }
.input-group .input { padding-right: 44px; }
.input-group .btn-icon { position: absolute; right: 4px; top: 4px; height: 36px; width: 36px; }
.checkbox { display: flex; align-items: center; gap: var(--s2); cursor: pointer; color: var(--text-muted); }
input[type='range'], input[type='checkbox'] { accent-color: var(--primary); }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: var(--s5); }
.card-elevated { background: var(--surface-2); box-shadow: var(--shadow); }
.card-hover { transition: transform .18s var(--ease), border-color .18s, box-shadow .18s; }
.card-hover:hover { transform: translateY(-3px); border-color: var(--border-strong); box-shadow: var(--shadow); }
.card-title { font-size: 16px; font-weight: 700; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; }
.panel-header { padding: var(--s4) var(--s5); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: var(--s3); }
.panel-body { padding: var(--s5); }

/* Badges, chips, kbd */
.badge {
  white-space: nowrap; flex: none;
  display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 10px; border-radius: var(--radius-full);
  font-size: 12px; font-weight: 600; background: var(--surface-3); color: var(--text-muted); border: 1px solid var(--border);
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .9; }
.badge-plain::before { display: none; }
.badge-success { color: var(--success); background: rgba(47, 211, 160, .1); border-color: rgba(47, 211, 160, .25); }
.badge-warning { color: var(--warning); background: rgba(255, 181, 71, .1); border-color: rgba(255, 181, 71, .25); }
.badge-danger { color: var(--danger); background: rgba(255, 92, 122, .1); border-color: rgba(255, 92, 122, .25); }
.badge-info { color: var(--info); background: rgba(91, 157, 255, .1); border-color: rgba(91, 157, 255, .25); }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: var(--radius-full); background: var(--surface-2); border: 1px solid var(--border); font-size: 13px; }
.kbd { display: inline-block; min-width: 22px; padding: 1px 6px; border-radius: 6px; border: 1px solid var(--border-strong); border-bottom-width: 2px; background: var(--surface-2); font: 600 12px var(--font-mono); text-align: center; }

/* Avatar: <span class="avatar" style="--avatar-color:#6d7cff">CH</span>, or with an <img> inside */
.avatar {
  --avatar-size: 36px; --avatar-color: var(--primary);
  width: var(--avatar-size); height: var(--avatar-size); border-radius: 50%; flex: none; overflow: hidden;
  display: inline-grid; place-items: center; font-weight: 700; font-size: calc(var(--avatar-size) * .4); color: #fff;
  background: linear-gradient(135deg, var(--avatar-color), color-mix(in srgb, var(--avatar-color) 55%, #000));
  border: 2px solid rgba(255, 255, 255, .12);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-sm { --avatar-size: 28px; }
.avatar-lg { --avatar-size: 72px; }
.avatar-xl { --avatar-size: 112px; }

/* Tabs */
.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.tab { flex: 1; height: 38px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--text-muted); font: 600 14px var(--font); cursor: pointer; }
.tab[aria-selected='true'] { background: var(--surface-3); color: var(--text); box-shadow: var(--shadow); }

/* Dropdown menu (position it from JS relative to its trigger) */
.menu {
  position: absolute; min-width: 220px; padding: 6px; border-radius: var(--radius); z-index: 60;
  background: var(--surface-2); border: 1px solid var(--border-strong); box-shadow: var(--shadow-lg); animation: pop .14s var(--ease);
}
.menu-item { display: flex; align-items: center; gap: var(--s3); width: 100%; padding: 9px 12px; border: 0; border-radius: var(--radius-sm); background: none; color: var(--text); font: 500 14px var(--font); text-align: left; cursor: pointer; }
.menu-item:hover { background: var(--surface-3); text-decoration: none; }
.menu-item.danger { color: #ff8aa0; }
.menu-sep { height: 1px; margin: 6px 4px; background: var(--border); }

/* Modal: <div class="modal-backdrop"><div class="modal" role="dialog" aria-modal="true">…</div></div> */
.modal-backdrop { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: var(--s4); background: var(--overlay); backdrop-filter: blur(6px); animation: fade .15s; }
.modal { width: 100%; max-width: 520px; max-height: calc(100dvh - 32px); overflow: auto; background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: var(--radius-xl); box-shadow: var(--shadow-lg); animation: pop .18s var(--ease); }
.modal-lg { max-width: 860px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); padding: var(--s5) var(--s5) 0; }
.modal-body { padding: var(--s5); }
.modal-footer { display: flex; justify-content: flex-end; gap: var(--s3); padding: 0 var(--s5) var(--s5); }

/* Toasts: <div class="toast-stack"><div class="toast toast-success">…</div></div> */
.toast-stack { position: fixed; right: var(--s5); bottom: var(--s5); z-index: 200; display: flex; flex-direction: column; gap: var(--s2); max-width: min(380px, calc(100vw - 32px)); }
.toast { padding: 12px 16px; border-radius: var(--radius); background: var(--surface-3); border: 1px solid var(--border-strong); border-left: 3px solid var(--info); box-shadow: var(--shadow-lg); animation: slide-in .2s var(--ease); font-size: 14px; }
.toast-success { border-left-color: var(--success); }
.toast-error { border-left-color: var(--danger); }
.toast-warning { border-left-color: var(--warning); }

/* Alerts / banners */
.alert { display: flex; gap: var(--s3); align-items: flex-start; padding: 12px 16px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface-2); font-size: 14px; }
.alert-warning { border-color: rgba(255, 181, 71, .3); background: rgba(255, 181, 71, .08); color: #ffd08a; }
.alert-danger { border-color: rgba(255, 92, 122, .3); background: rgba(255, 92, 122, .08); color: #ffa3b5; }
.alert-info { border-color: rgba(91, 157, 255, .3); background: rgba(91, 157, 255, .08); color: #a9c8ff; }
.alert-success { border-color: rgba(47, 211, 160, .3); background: rgba(47, 211, 160, .08); color: #8ef0cf; }

/* Progress, spinner, skeleton, empty state */
.progress { height: 8px; border-radius: var(--radius-full); background: var(--surface-3); overflow: hidden; }
.progress > span { display: block; height: 100%; width: 0; background: var(--primary-grad); border-radius: inherit; transition: width .2s; }
.spinner { width: 20px; height: 20px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .2); border-top-color: #fff; animation: spin .7s linear infinite; }
.skeleton { border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
.empty-state { display: flex; flex-direction: column; align-items: center; gap: var(--s3); padding: var(--s8) var(--s5); text-align: center; color: var(--text-muted); border: 1px dashed var(--border-strong); border-radius: var(--radius-xl); }
.empty-state .icon { font-size: 40px; }

/* Status dot */
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); display: inline-block; }
.dot-live { background: var(--success); box-shadow: 0 0 0 4px rgba(47, 211, 160, .18); animation: pulse 1.6s infinite; }
.dot-warn { background: var(--warning); }
.dot-error { background: var(--danger); }

@keyframes fade { from { opacity: 0; } }
@keyframes fade-up { from { opacity: 0; transform: translateY(6px); } }
@keyframes pop { from { opacity: 0; transform: scale(.97); } }
@keyframes slide-in { from { opacity: 0; transform: translateX(16px); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes pulse { 50% { box-shadow: 0 0 0 7px rgba(47, 211, 160, 0); } }

@media (max-width: 640px) {
  .container { padding: 0 var(--s4); }
  .page { padding-top: var(--s5); padding-bottom: var(--s7); }
  .navbar .container { gap: var(--s3); }
  .btn-sm { height: 40px; }
  .btn-icon.btn-sm { width: 40px; }
  .tab { height: 40px; }
  .menu-item { min-height: 44px; }
  .navbar .nav-links { display: none; }
  .modal-backdrop { align-items: end; padding: 0; }
  .modal { border-radius: var(--radius-xl) var(--radius-xl) 0 0; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* Game area wrapper: holds the screen and the touch controls; this is what goes full screen. */
.game-shell { position: relative; width: 100%; display: flex; justify-content: center; scroll-margin-top: calc(var(--nav-h) + 16px); }
.game-shell:fullscreen,
.game-shell.pseudo-fullscreen {
  position: fixed; inset: 0; z-index: 90; width: 100vw; height: 100dvh;
  display: grid; place-items: center; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: #000;
}
.game-shell:fullscreen > .stage-4x3,
.game-shell.pseudo-fullscreen > .stage-4x3 {
  width: min(100vw, 100dvh * 4 / 3) !important; max-width: none !important; height: auto; max-height: 100dvh;
  border: 0; border-radius: 0; box-shadow: none;
}
body.has-pseudo-fullscreen { overflow: hidden; }
/* Mobile fullscreen retains an accessible escape action inside the game shell. */
.chidol-mobile-exit,
.chidol-mobile-rotate-hint { display: none; }
.game-shell:fullscreen .chidol-mobile-exit,
.game-shell.pseudo-fullscreen .chidol-mobile-exit {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  z-index: 26;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  color: #fff;
  background: rgba(12, 17, 24, .27);
  border: 1.5px solid rgba(255,255,255,.6);
  border-radius: 10px;
  cursor: pointer;
  touch-action: manipulation;
}
@media (orientation: portrait) and (pointer: coarse) {
  .game-shell:fullscreen .chidol-mobile-rotate-hint,
  .game-shell.pseudo-fullscreen .chidol-mobile-rotate-hint {
    position: absolute;
    top: calc(env(safe-area-inset-top, 0px) + 65px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 25;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 12px;
    max-width: 85vw;
    color: white;
    background: rgba(12,17,24,.78);
    text-align: center;
    font-size: 12px;
    pointer-events: none;
  }
}

/* Portrait phones: reserve room under the screen for the on-screen controller. */
@media (orientation: portrait) {
  .game-shell.touch-on:not(:fullscreen):not(.pseudo-fullscreen) { padding-bottom: clamp(250px, 66vw, 340px); }
}
