:root {
  color-scheme: dark;
  --felt-base: #11221b;
  --felt-deep: #08120e;
  --felt-radial: #1a382c;
  --surface: #0e1a15;
  --surface-raised: #142820;
  --surface-overlay: #193127;
  --border: #284436;
  --border-light: #3d6652;
  --border-gold: #c9a656;
  --gold: #f3d38f;
  --gold-bright: #ffe2a8;
  --gold-glow: rgba(243, 211, 143, 0.28);
  --gold-gradient: linear-gradient(135deg, #fce4b3 0%, #e0bd72 100%);
  --gold-surface: #2d2617;
  --text: #f5eedc;
  --muted: #a0b3a6;
  --ink: #1a160d;
  --danger: #e26161;
  --danger-surface: #381a1a;
  --danger-border: #793434;
  --success: #55c285;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 2px 6px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.55);
  --shadow-glow: 0 0 16px var(--gold-glow);
  --shadow-screen: 0 0 0 1px rgba(243, 211, 143, 0.12), 0 14px 42px rgba(0, 0, 0, 0.75), 0 2px 8px rgba(0, 0, 0, 0.5);
  --font-ui: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "Roboto Mono", Menlo, Monaco, Consolas, monospace;
  font: 1rem/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

body.account-page {
  overflow: auto;
  user-select: auto;
  -webkit-user-select: auto;
}

.account-header {
  background: var(--felt-base);
  border-bottom: 1px solid var(--border);
}

.account-header-inner,
.account-panel {
  width: min(100% - 2rem, 50rem);
  margin-inline: auto;
}

.account-header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.375rem 1rem;
  min-height: 4rem;
  padding-block: 0.5rem;
}

.account-brand,
.account-game {
  display: inline-flex;
  align-items: center;
  min-height: 2.75rem;
  font-size: 0.875rem;
  font-weight: 650;
}

.account-brand {
  gap: 0.625rem;
  color: var(--text);
  text-decoration: none;
}

.account-game {
  color: var(--gold);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
}

.account-panel {
  padding-block: clamp(1.75rem, 5vw, 3.5rem);
}

.account-panel > h1 {
  margin-bottom: 1.125rem;
  font-size: clamp(1.875rem, 5vw, 2.5rem);
  line-height: 1.12;
  letter-spacing: -0.03em;
}

.account-identity {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 1rem 0 1.5rem;
  border-bottom: 1px solid var(--border);
}

.account-person {
  display: flex;
  align-items: center;
  gap: 0.875rem;
  min-width: 0;
  overflow-wrap: anywhere;
}

.account-avatar {
  position: relative;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 3rem;
  height: 3rem;
  overflow: hidden;
  border-radius: 50%;
  background: var(--surface-overlay);
  color: var(--gold);
  font-size: 0.875rem;
  font-weight: 700;
}

.account-avatar img {
  position: absolute;
  inset: 0;
  object-fit: cover;
}

.account-person h2,
.account-backup h2 {
  margin: 0;
  font-size: 1.125rem;
  line-height: 1.3;
}

.account-person p,
.account-description {
  margin: 0.25rem 0 0;
  color: var(--muted);
}

.account-person p {
  font-size: 0.875rem;
}

.account-backup {
  margin-top: 1.5rem;
  padding: clamp(1rem, 4vw, 1.5rem);
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}

.account-description {
  max-width: 58ch;
}

.account-backup-record {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.25rem 1rem;
  margin-block: 1.25rem;
  padding-block: 0.75rem;
  border-block: 1px solid var(--border);
  font-size: 0.9375rem;
}

#account-backup-date { font-variant-numeric: tabular-nums; }
.account-backup-record > :first-child { color: var(--muted); }

.account-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.account-actions button { min-height: 2.75rem; }

#account-backup-status {
  margin: 0.875rem 0 0;
  color: var(--text);
}

#account-widget {
  min-width: 0;
  max-width: 100%;
}

@media (max-width: 440px) {
  .account-backup .account-actions button { flex: 1 1 100%; }
}

/* Overall game table container */
.table {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;
  height: 100dvh;
  background: var(--surface);
  position: relative;
}

.toolbar, .table-footer {
  padding-inline: max(1rem, env(safe-area-inset-left)) max(1rem, env(safe-area-inset-right));
}

/* Top toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  padding-block: max(0.5rem, env(safe-area-inset-top)) 0.5rem;
  background: linear-gradient(180deg, #12211b 0%, #0d1713 100%);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-badge {
  display: grid;
  place-items: center;
  padding: 2px;
  background: var(--surface-raised);
  border: 1px solid var(--border-gold);
  border-radius: var(--radius-sm);
  box-shadow: 0 0 10px rgba(243, 211, 143, 0.2);
}

.brand-badge img {
  display: block;
  border-radius: 4px;
}

.brand-info {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}

h1 {
  margin: 0;
  font-size: clamp(1rem, 2.5vw, 1.25rem);
  font-weight: 800;
  letter-spacing: 0.02em;
}

h1 span {
  background: var(--gold-gradient);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.version-tag {
  display: inline-block;
  padding: 0.1em 0.45em;
  border: 1px solid rgba(243, 211, 143, 0.4);
  border-radius: 999px;
  background: var(--gold-surface);
  color: var(--gold);
  font: 700 0.7rem/1.2 var(--font-mono);
  letter-spacing: 0.04em;
  vertical-align: middle;
}

/* Toolbar actions & buttons */
.actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

button {
  min-height: 2.6rem;
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: linear-gradient(180deg, #1b3026 0%, #12221b 100%);
  color: var(--text);
  font: 650 0.875rem/1.2 var(--font-ui);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.08);
  transition: background 0.18s ease, border-color 0.18s ease, transform 0.1s ease, box-shadow 0.18s ease;
}

button:hover {
  background: linear-gradient(180deg, #244134 0%, #172c23 100%);
  border-color: var(--border-light);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.14);
}

button:active {
  transform: translateY(1px);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.45), inset 0 1px 2px rgba(0, 0, 0, 0.25);
}

button:disabled {
  cursor: wait;
  opacity: 0.6;
}

button.primary {
  background: var(--gold-gradient);
  border-color: #d1ad5c;
  color: var(--ink);
  font-weight: 750;
  box-shadow: 0 2px 8px rgba(243, 211, 143, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

button.primary:hover {
  background: linear-gradient(135deg, #fff0cb 0%, #edd087 100%);
  border-color: #e4c16f;
  box-shadow: 0 4px 14px rgba(243, 211, 143, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

button.danger {
  background: linear-gradient(180deg, var(--danger-surface) 0%, #281212 100%);
  border-color: var(--danger-border);
  color: #ffcccc;
}

button.danger:hover {
  background: linear-gradient(180deg, #522424 0%, #361717 100%);
  border-color: #a84848;
}

:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
}

.toolbar-icon {
  flex: 0 0 1.125rem;
  width: 1.125rem;
  height: 1.125rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#account-open-status {
  flex-basis: 100%;
  margin: 0;
  font-size: 0.875rem;
  color: var(--text);
}

/* Center stage & game canvas */
.stage {
  display: grid;
  place-items: center;
  container-type: size;
  padding: clamp(0.35rem, 1.4vmin, 1rem)
    max(clamp(0.35rem, 1.4vmin, 1rem), env(safe-area-inset-right))
    clamp(0.35rem, 1.4vmin, 1rem)
    max(clamp(0.35rem, 1.4vmin, 1rem), env(safe-area-inset-left));
  background: radial-gradient(circle at 50% 48%, var(--felt-radial) 0%, #152920 50%, var(--felt-deep) 100%);
  position: relative;
  overflow: hidden;
  contain: layout paint;
}

/* Subtle table edge vignette */
.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse at center, transparent 65%, rgba(4, 10, 7, 0.7) 100%);
}

.screen {
  position: relative;
  width: min(100cqi, calc(100cqb * 16 / 9));
  max-width: 100%;
  aspect-ratio: 16 / 9;
  border: 1px solid rgba(75, 107, 88, 0.75);
  border-radius: var(--radius-md);
  background: var(--felt-deep);
  box-shadow: var(--shadow-screen);
  contain: layout paint;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: calc(var(--radius-md) - 1px);
  touch-action: none;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
}

canvas.pending { visibility: hidden; }

/* Loading screen */
.loading {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 0.75rem;
  overflow: auto;
  padding: 1rem;
  border-radius: inherit;
  text-align: center;
  background: radial-gradient(circle at 50% 45%, #162920 0%, var(--surface) 80%);
  z-index: 2;
}

.loading-art {
  position: relative;
  display: grid;
  place-items: center;
}

.loading-logo {
  width: clamp(2.5rem, 14cqi, 4.5rem);
  height: auto;
  border-radius: 8px;
  animation: logoPulse 2.4s ease-in-out infinite;
}

.loading-glow {
  position: absolute;
  inset: -10px;
  background: radial-gradient(circle, var(--gold-glow) 0%, transparent 70%);
  filter: blur(8px);
  pointer-events: none;
}

@keyframes logoPulse {
  0%, 100% { transform: scale(1); filter: drop-shadow(0 0 8px rgba(243, 211, 143, 0.3)); }
  50% { transform: scale(1.05); filter: drop-shadow(0 0 18px rgba(243, 211, 143, 0.6)); }
}

.loading p#status {
  margin: 0;
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
}

/* Download progress bar */
.progress-track {
  width: min(280px, 85%);
  height: 8px;
  background: rgba(5, 12, 9, 0.7);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.6);
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--gold-gradient);
  border-radius: 999px;
  box-shadow: 0 0 10px var(--gold-glow);
  transition: width 0.2s cubic-bezier(0.1, 0.85, 0.25, 1);
}

.loading-tip {
  margin: 0.25rem 0 0;
  font-size: 0.8125rem;
  color: var(--muted);
  max-width: 44ch;
  line-height: 1.4;
}

/* Mobile landscape prompt */
.landscape-prompt {
  display: none;
  justify-items: center;
  gap: 0.75rem;
  padding-inline: 1rem;
  text-align: center;
  z-index: 5;
}

.rotate-icon-wrapper {
  color: var(--gold);
  animation: rotateHint 2.4s ease-in-out infinite;
}

@keyframes rotateHint {
  0%, 100% { transform: rotate(0deg); }
  40%, 60% { transform: rotate(-90deg); }
}

.landscape-prompt p {
  max-width: 32ch;
  min-height: 2.8em;
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
  line-height: 1.4;
}

/* Bottom footer */
.table-footer {
  padding-block: 0.4rem max(0.4rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: linear-gradient(180deg, #0d1713 0%, #0a130f 100%);
  color: var(--muted);
  font-size: 0.8125rem;
  text-align: center;
}

kbd {
  display: inline-block;
  padding: 0.15em 0.45em;
  border: 1px solid var(--border);
  border-bottom: 2px solid #233b2f;
  border-radius: 4px;
  background: #172820;
  color: var(--gold);
  font: 650 0.8125em/1.3 var(--font-mono);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.keyboard-hint kbd:not(:first-child) {
  margin-inline-start: 0.85em;
}

/* Modern dialog modal styles */
dialog {
  width: min(30rem, calc(100% - 2rem));
  max-height: calc(100% - 2rem);
  padding: 1.35rem;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  background: var(--surface-raised);
  color: var(--text);
  box-shadow: var(--shadow-md), 0 0 0 1px rgba(243, 211, 143, 0.1);
  /* Smooth open/close transitions */
  transition: display 0.2s allow-discrete, overlay 0.2s allow-discrete, opacity 0.2s ease-out, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  opacity: 0;
  transform: scale(0.96) translateY(8px);
}

dialog[open] {
  opacity: 1;
  transform: scale(1) translateY(0);
}

@starting-style {
  dialog[open] {
    opacity: 0;
    transform: scale(0.96) translateY(8px);
  }
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgba(6, 13, 10, 0.82);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: display 0.2s allow-discrete, overlay 0.2s allow-discrete, opacity 0.2s ease-out;
  opacity: 0;
}

dialog[open]::backdrop {
  opacity: 1;
}

@starting-style {
  dialog[open]::backdrop {
    opacity: 0;
  }
}

.dialog-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.85rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

dialog h2 {
  margin: 0;
  color: var(--gold);
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.dialog-close-btn {
  min-height: 2rem;
  width: 2rem;
  padding: 0;
  border-radius: 50%;
  font-size: 0.875rem;
  color: var(--muted);
}

.dialog-close-btn:hover {
  color: var(--text);
  border-color: var(--border-light);
}

dialog dl { margin: 0; }
dialog dl div {
  padding: 0.75rem 0;
  border-top: 1px solid var(--border);
}
dialog dt {
  font-weight: 750;
  color: var(--text);
}
dialog dd {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.9375rem;
  line-height: 1.4;
}

dialog p {
  margin: 0.75rem 0 1rem;
  color: var(--muted);
  line-height: 1.45;
}

.audio-hint {
  font-size: 0.875rem;
  color: var(--muted);
  font-style: italic;
}

.install-help {
  padding-block-start: 0.75rem;
  border-top: 1px solid var(--border);
}

.install-help h3 {
  margin: 0;
  font-size: 1rem;
  color: var(--gold);
}

.install-help p {
  margin-block-start: 0.35rem;
  font-size: 0.875rem;
}

.dialog-back-btn {
  width: 100%;
  margin-top: 0.5rem;
}

/* Updates dialog */
.updates-dialog[open] {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: min(34rem, calc(100% - 2rem));
}

.release-list {
  min-height: 0;
  max-height: 60vh;
  max-height: 60dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 0.35rem;
}

.release-list::-webkit-scrollbar {
  width: 6px;
}
.release-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}

.release {
  padding-block: 0.85rem;
  border-top: 1px solid var(--border);
}

.release-meta {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.release h3 {
  margin: 0;
  font-size: 1.0625rem;
  color: var(--gold);
  font-weight: 750;
}

.release-badge {
  padding: 0.15em 0.5em;
  border-radius: 999px;
  font: 700 0.6875rem/1.2 var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.release-badge.latest {
  background: rgba(85, 194, 133, 0.18);
  border: 1px solid rgba(85, 194, 133, 0.5);
  color: var(--success);
}

.release time {
  color: var(--muted);
  font-size: 0.8125rem;
}

.release ul {
  margin: 0.6rem 0 0;
  padding-inline-start: 1.25rem;
  color: var(--text);
  font-size: 0.9375rem;
}

.release li + li {
  margin-block-start: 0.45rem;
}

.release li strong {
  color: var(--gold-bright);
}

/* Save Backup dialog */
.backup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

#backup-status:empty { margin: 0; }

.backup-review {
  margin-block-start: 1rem;
  padding-block-start: 0.75rem;
  border-top: 1px solid var(--border);
}

.backup-review h3 { margin: 0; font-size: 1rem; color: var(--gold); }
.backup-review h3:focus { outline: none; }
.backup-review p { overflow-wrap: anywhere; }
.backup-review ul {
  margin-block: 0.5rem;
  padding-inline-start: 1.25rem;
  font-size: 0.875rem;
}

@media (orientation: landscape) and (max-height: 480px) {
  #backups { padding: 0.875rem 1.25rem; }
  #backups p { margin-block: 0.5rem 0.75rem; }
}

/* Mobile responsive layout (< 480px) */
@media (max-width: 480px) {
  .toolbar { gap: 0.4rem; }
  .brand-badge img { width: 30px; height: 30px; }
  h1 { font-size: 0.9375rem; }
  .actions button { padding-inline: 0.55rem; font-size: 0.8125rem; }
  .version-tag { display: none; }
}

/* Mobile full-bleed game mode */
.table[data-mobile] {
  position: relative;
  grid-template-rows: minmax(0, 1fr);
}

[data-mobile] .toolbar {
  position: absolute;
  z-index: 10;
  inset: calc(env(safe-area-inset-top) + 0.5rem) auto auto calc(env(safe-area-inset-left) + 0.5rem);
  padding: 0;
  background: transparent;
  border: 0;
}

[data-mobile] .brand, [data-mobile] .table-footer { display: none; }

.mobile-menu {
  display: grid;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  place-items: center;
  background: rgba(16, 29, 23, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-light);
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.menu-dots {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: -6px 0 var(--gold), 6px 0 var(--gold);
}

.actions[popover] {
  inset: calc(env(safe-area-inset-top) + 3.75rem) auto auto calc(env(safe-area-inset-left) + 0.5rem);
  flex-direction: column;
  align-items: stretch;
  gap: 0.35rem;
  margin: 0;
  max-height: calc(100dvh - 4.5rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow-y: auto;
  flex-wrap: nowrap;
  padding: 0.65rem;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  background: rgba(18, 33, 26, 0.94);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: var(--shadow-md);
}

.actions[popover]:not(:popover-open) { display: none; }
.actions[popover] button {
  padding-inline: 1rem;
  justify-content: flex-start;
  min-height: 2.75rem;
}

[data-mobile] .stage {
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--surface);
}

[data-mobile] .screen {
  width: 100%;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

[data-mobile] canvas { border-radius: 0; }
[data-mobile] canvas:focus-visible { outline-offset: -3px; }

@media (orientation: landscape) {
  [data-mobile] .stage { grid-template: minmax(0, 1fr) / minmax(0, 1fr); }
  [data-mobile] .screen { height: 100%; aspect-ratio: auto; }
}

@media (orientation: portrait) and (max-width: 900px) {
  .table {
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .stage {
    container-type: inline-size;
    align-content: center;
    gap: 1.25rem;
    padding-block: 1.25rem max(1.25rem, env(safe-area-inset-bottom));
  }

  .screen { width: 100%; }
  .table-footer { display: none; }
  .landscape-prompt { display: grid; }
}

@media (orientation: portrait) {
  [data-mobile] .stage {
    container-type: inline-size;
    align-content: center;
    gap: 1.25rem;
    padding-block: calc(env(safe-area-inset-top) + 3.75rem) max(1.25rem, env(safe-area-inset-bottom));
  }

  [data-mobile] .landscape-prompt { display: grid; }
}

@media (prefers-reduced-motion: reduce) {
  *, ::before, ::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
