/* Pokémon Chess v2 shell: design tokens, layout, header and tab bar, buttons, forms, cards,
   dialogs and toasts. The board and game layout live in game.css. Dark is the v1 look and the
   default; a light theme follows prefers-color-scheme. */

/* ---------- Tokens ---------- */
:root {
  color-scheme: dark;

  --bg: #302e2b;
  --bg-grad: linear-gradient(to right, #302e2b, #4e4e4e, #302e2b);   /* v1 stylesheet:6-7 */
  --surface: #3a3835;
  --surface-2: #45423e;
  --surface-3: #524e49;
  --border: #625d56;
  --border-soft: rgba(255, 255, 255, 0.1);
  --text: #ffffff;
  --muted: #c9c9c9;
  --link: #add8e6;                       /* v1 "lightblue" */
  --accent: #5f7f3e;                     /* v1 #779556, deepened so white text passes 4.5:1 */
  --accent-hover: #4d6a31;
  --accent-text: #ffffff;
  --brand-green: #779556;                /* v1 dark square and button green */
  --brand-cream: #ebecd0;                /* v1 light square */
  --danger: #d33c3a;
  --danger-hover: #b52f2d;
  --danger-text: #ff9a97;
  --ok: #6ab04c;
  --ok-text: #9ad67d;
  --warn: #e5a50a;
  --guest: #8e8e8e;
  --focus: #3498db;                      /* v1 selected-square blue */
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 18px rgba(0, 0, 0, 0.22);
  --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.5);

  --chrome-bg: rgba(0, 0, 0, 0.35);      /* header */
  --chrome-solid: #1f1e1c;               /* tab bar */
  --chrome-text: #ffffff;
  --chrome-muted: #bdb9b2;
  --input-bg: #2a2826;
  --btn-bg: #4a4743;
  --btn-bg-hover: #57534e;
  --btn-text: #ffffff;
  --switch-off: #6b6761;
  --toast-bg: #1f1e1c;
  --toast-text: #ffffff;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --gap: 12px;
  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 14px;
  --radius-pill: 999px;

  --header-h: 56px;
  --tabbar-h: 56px;
  --tap: 44px;
  --content-w: 1100px;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;

    --bg: #f3f1ec;
    --bg-grad: linear-gradient(to right, #e8e5dd, #f7f6f2, #e8e5dd);
    --surface: #ffffff;
    --surface-2: #f2f0ea;
    --surface-3: #e6e3db;
    --border: #cfcabf;
    --border-soft: rgba(0, 0, 0, 0.08);
    --text: #1f1e1c;
    --muted: #5b5750;
    --link: #0a5a8a;
    --danger-text: #b3261e;
    --ok-text: #2f6b1c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.07), 0 6px 18px rgba(0, 0, 0, 0.07);
    --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.22);

    --chrome-bg: #302e2b;                /* the header stays the dark brand bar */
    --chrome-solid: #302e2b;
    --input-bg: #ffffff;
    --btn-bg: #e6e3db;
    --btn-bg-hover: #d9d5cb;
    --btn-text: #1f1e1c;
    --switch-off: #aaa59a;
    --toast-bg: #2b2a27;
  }
}

/* ---------- Base ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overflow-x: hidden;                    /* a last guard against sideways scrolling on phones */
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  background-color: var(--bg);
  background-image: var(--bg-grad);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  overflow-wrap: break-word;
  -webkit-tap-highlight-color: transparent;
}

img,
svg,
canvas,
video {
  max-width: 100%;
}

a {
  color: var(--link);
  text-underline-offset: 2px;
}

h1,
h2,
h3 {
  line-height: 1.2;
  margin: 0 0 var(--space-3);
}

h1 {
  font-size: 1.6rem;
}

h2 {
  font-size: 1.25rem;
}

h3 {
  font-size: 1.05rem;
}

p {
  margin: 0 0 var(--space-3);
}

pre,
code {
  font-family: var(--font-mono);
  font-size: 0.875rem;
}

[hidden],
.hidden {
  display: none !important;
}

dialog:not([open]) {
  display: none;
}

/* Focus rings: always for keyboards; browsers without :focus-visible drop the second rule and
   show the ring on every focus, which is fine. */
:focus {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

:focus:not(:focus-visible) {
  outline: none;
}

.view:focus,
[tabindex="-1"]:focus {
  outline: none;
}

.skip-link {
  position: absolute;
  z-index: 100;
  left: 8px;
  top: -100px;
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.skip-link:focus {
  top: 8px;
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: var(--gap);
  height: var(--header-h);
  padding: 0 var(--space-4);
  background: var(--chrome-bg);
  color: var(--chrome-text);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  min-height: var(--tap);
  color: inherit;
  text-decoration: none;
}

.brand-logo {
  display: block;
  flex: none;
  width: auto;
  height: 40px;
}

.brand-name {
  min-width: 0;
  font-weight: 700;
  font-size: 1.125rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-nav {
  margin-left: auto;
  display: flex;
  gap: var(--space-1);
}

.app-nav a {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  padding: 0 14px;
  border-radius: var(--radius-pill);
  color: var(--chrome-text);
  font-weight: 600;
  text-decoration: none;
}

.app-nav a[aria-current="page"] {
  background: rgba(255, 255, 255, 0.14);
  box-shadow: inset 0 -3px 0 var(--brand-green);
}

/* ---------- Notices ---------- */
.notices:empty {
  display: none;
}

.notices {
  width: min(100% - 24px, var(--content-w));
  margin: var(--space-3) auto 0;
  display: grid;
  gap: var(--space-2);
}

.notice {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--surface);
  border-left: 4px solid var(--focus);
  box-shadow: var(--shadow);
}

.notice > :first-child {
  flex: 1;
  min-width: 0;
  margin: 0;
}

.notice.warn {
  border-left-color: var(--warn);
}

.notice.error {
  border-left-color: var(--danger);
}

/* ---------- Main view ---------- */
.view {
  flex: 1 0 auto;
  width: min(100% - 24px, var(--content-w));
  margin: var(--space-4) auto var(--space-5);
}

.view-loading,
.view-error {
  margin: 0;
  padding: 48px 0;
  text-align: center;
  color: var(--muted);
}

.view-loading {
  animation: pc-fade-in 0.2s ease-out 0.3s both;
}

/* ---------- Tab bar (phones) ---------- */
.tabbar {
  display: none;
}

.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--tap);
  color: var(--chrome-muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-decoration: none;
}

.tabbar a[aria-current="page"] {
  color: var(--chrome-text);
  box-shadow: inset 0 3px 0 var(--brand-green);
}

.tabbar svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tabbar svg .fill {
  fill: currentColor;
  stroke: none;
  opacity: 0.4;
}

/* ---------- Footer ---------- */
.app-footer {
  padding: var(--space-4) var(--space-4) var(--space-5);
  border-top: 1px solid var(--border-soft);
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

.footer-links {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  column-gap: var(--space-5);
}

.footer-links li {
  padding: 6px 0;
}

.footer-links a {
  display: inline-block;
  padding: 4px 0;
}

.noscript {
  margin: var(--space-4);
  padding: var(--space-4);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
}

/* ---------- Text and layout helpers ---------- */
.muted {
  color: var(--muted);
}

.small {
  font-size: 0.875rem;
}

.center {
  text-align: center;
}

.lead {
  font-size: 1.1rem;
  color: var(--muted);
}

.sr-only,
.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.spread {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

.actions,
.btn-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.actions {
  margin-top: var(--space-4);
}

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-4);
}

.page-head h1 {
  margin: 0;
}

.empty,
.empty-state {
  padding: var(--space-6) var(--space-4);
  text-align: center;
  color: var(--muted);
}

.empty-state .btn {
  margin-top: var(--space-2);
}

/* ---------- Buttons (at least 44 px tall) ---------- */
/* :where() keeps plain <button> styling at zero specificity, so any component class wins. */
:where(button),
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--tap);
  padding: 10px 18px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: var(--btn-bg);
  color: var(--btn-text);
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  transition: background-color 0.12s, border-color 0.12s, transform 0.06s;
}

a.btn {
  color: var(--btn-text);
}

.btn-primary,
.btn.primary,
button.primary,
a.btn-primary,
a.btn.primary {
  background: var(--accent);
  color: var(--accent-text);
}

.btn-danger,
.btn.danger,
button.danger,
a.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-ghost,
.btn.ghost,
button.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.btn-block,
.btn.block {
  display: flex;
  width: 100%;
}

.btn-lg,
.btn.big {
  min-height: 56px;
  padding: 14px 24px;
  font-size: 1.1rem;
}

.icon-btn {
  min-width: var(--tap);
  padding: 0;
}

@media (hover: hover) {
  :where(button:hover),
  .btn:hover {
    background: var(--btn-bg-hover);
  }

  .btn-primary:hover,
  .btn.primary:hover,
  button.primary:hover {
    background: var(--accent-hover);
  }

  .btn-danger:hover,
  .btn.danger:hover,
  button.danger:hover {
    background: var(--danger-hover);
  }

  .btn-ghost:hover,
  .btn.ghost:hover,
  button.ghost:hover {
    background: var(--surface-2);
  }
}

:where(button:active),
.btn:active {
  transform: translateY(1px);
}

:where(button:disabled),
.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ---------- Forms ---------- */
:where(input:not([type]), input[type="text"], input[type="search"], input[type="password"],
  input[type="email"], input[type="url"], input[type="number"], input[type="tel"], select, textarea) {
  max-width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 16px;                       /* 16 px stops iOS zooming in on focus */
}

:where(textarea) {
  min-height: 96px;
  resize: vertical;
}

:where(input, select, textarea)::placeholder {
  color: var(--muted);
  opacity: 0.8;
}

:where(input[type="checkbox"], input[type="radio"]) {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--accent);
}

[aria-invalid="true"] {
  border-color: var(--danger);
}

.field {
  display: grid;
  gap: 6px;
  margin-bottom: var(--space-4);
}

.field > label,
.field > .label {
  font-weight: 600;
}

.field :where(input, select, textarea) {
  width: 100%;
}

.hint {
  color: var(--muted);
  font-size: 0.875rem;
}

.field-error {
  color: var(--danger-text);
  font-size: 0.9rem;
}

/* A checkbox row with a 44 px tap area: <label class="check"><input type="checkbox"> Text</label> */
.check {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: var(--tap);
  cursor: pointer;
}

/* A switch: <label class="switch"><span>No RNG</span><input type="checkbox"></label> */
.switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: var(--tap);
  cursor: pointer;
}

.switch input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  width: 48px;
  height: 28px;
  margin: 0;
  border-radius: var(--radius-pill);
  background: var(--switch-off) radial-gradient(circle at 14px 50%, #fff 0 10px, transparent 10.5px) no-repeat;
  cursor: pointer;
}

.switch input[type="checkbox"]:checked {
  background: var(--accent) radial-gradient(circle at 34px 50%, #fff 0 10px, transparent 10.5px) no-repeat;
}

/* ---------- Cards, lists, pills ---------- */
.card {
  padding: var(--space-4);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.card > :last-child {
  margin-bottom: 0;
}

.card-title {
  margin: 0 0 var(--space-2);
  font-size: 1.1rem;
}

.cards {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr));
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: var(--space-2);
}

.list-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border-soft);
}

.list-item > .grow {
  flex: 1 1 160px;
  min-width: 0;
}

.pill,
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  border-radius: var(--radius-pill);
  background: var(--surface-3);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
}

/* Side pills: always the piece colours, with an edge so each shows on either theme. */
.pill.light {
  background: #f4f4f4;
  color: #1f1e1c;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.pill.dark {
  background: #1f1e1c;
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.3);
}

.pill.guest {
  background: var(--guest);
  color: #fff;
}

/* A strip of small colour squares, e.g. a team's 16 types. */
.swatches {
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 1fr));
  gap: 2px;
  max-width: 320px;
}

.swatches > * {
  display: block;
  aspect-ratio: 1 / 1;
  border-radius: 3px;
  background: var(--surface-3);
}

/* ---------- Home ---------- */
.home {
  display: grid;
  gap: var(--space-5);
  max-width: 880px;
  margin: 0 auto;
}

.hero {
  text-align: center;
}

.hero-logo {
  display: block;
  width: auto;
  height: 220px;
  margin: 0 auto var(--space-3);
}

.hero-title {
  font-size: 2rem;
  margin-bottom: var(--space-2);
}

.hero .lead {
  max-width: 36em;
  margin: 0 auto;
}

.preview-note {
  margin: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius);
  background: var(--surface);
  border-left: 4px solid var(--brand-green);
  box-shadow: var(--shadow);
}

.big-links {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.big-link {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  min-height: 96px;
  padding: var(--space-4) var(--space-5);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-soft);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  box-shadow: var(--shadow);
  transition: background-color 0.12s, transform 0.06s;
}

.big-link.primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-text);
}

.big-link-title {
  font-size: 1.2rem;
  font-weight: 700;
}

.big-link-sub {
  font-size: 0.9rem;
  opacity: 0.85;
}

@media (hover: hover) {
  .big-link:hover {
    background: var(--surface-2);
  }

  .big-link.primary:hover {
    background: var(--accent-hover);
  }
}

.big-link:active {
  transform: translateY(1px);
}

.how-to summary {
  min-height: var(--tap);
  display: flex;
  align-items: center;
  font-weight: 700;
  cursor: pointer;
}

.how-to ul {
  margin: var(--space-2) 0 0;
  padding-left: 1.25em;
}

.how-to li + li {
  margin-top: 6px;
}

/* ---------- Error card ---------- */
.error-card {
  max-width: 560px;
  margin: var(--space-6) auto;
  border-left: 4px solid var(--danger);
}

.error-title {
  font-size: 1.35rem;
}

.error-details {
  margin-top: var(--space-4);
  color: var(--muted);
}

.error-details summary {
  cursor: pointer;
  min-height: var(--tap);
  display: flex;
  align-items: center;
}

.error-details pre {
  margin: var(--space-2) 0 0;
  padding: var(--space-3);
  max-height: 240px;
  overflow: auto;
  border-radius: var(--radius-sm);
  background: var(--input-bg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ---------- Dialog: a centred modal on desktop, a bottom sheet on phones ---------- */
.dialog {
  width: min(100% - 32px, 460px);
  max-width: none;
  max-height: min(100% - 32px, 760px);
  margin: auto;
  padding: 0;
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  overflow: auto;
  overscroll-behavior: contain;
}

.dialog.dialog-sheet {
  width: min(100% - 32px, 600px);
}

.dialog[open] {
  animation: pc-pop 0.16s ease-out;
}

/* ::backdrop does not inherit custom properties in older browsers: literal colours only. */
.dialog::backdrop {
  background: rgba(12, 11, 10, 0.6);
}

.dialog-inner {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding: 20px;
}

.dialog-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 28px;
}

.dialog-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.2rem;
}

.dialog-danger .dialog-title {
  color: var(--danger-text);
}

.dialog-x {
  flex: none;
  width: var(--tap);
  min-height: var(--tap);
  margin: -10px -10px -10px 0;
  padding: 0;
  border: 0;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--muted);
  font-size: 1.75rem;
  font-weight: 400;
  line-height: 1;
}

@media (hover: hover) {
  .dialog-x:hover {
    background: var(--surface-2);
    color: var(--text);
  }
}

.dialog-msg {
  margin: 0;
  white-space: pre-line;
}

label.dialog-msg {
  font-weight: 600;
}

.dialog-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.dialog-input {
  width: 100%;
}

.dialog-error {
  margin: -4px 0 0;
  color: var(--danger-text);
  font-size: 0.9rem;
}

.dialog-error:empty {
  display: none;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-2);
}

.dialog-actions > * {
  min-width: 96px;
}

.dialog-body {
  min-width: 0;
}

/* Without HTMLDialogElement (Safari before 15.4): a fixed box over a plain backdrop. */
.dialog.is-fallback {
  position: fixed;
  z-index: 1000;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation: none;
}

.dialog.is-fallback[open] {
  display: block;
}

.dialog-backdrop {
  position: fixed;
  z-index: 999;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(12, 11, 10, 0.6);
}

/* ---------- Toasts ---------- */
.toasts {
  position: fixed;
  z-index: 60;
  right: var(--space-4);
  bottom: var(--space-4);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-2);
  width: min(420px, calc(100% - 32px));
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 100%;
  padding: 8px 6px 8px 12px;
  border-radius: 10px;
  border-left: 4px solid var(--focus);
  background: var(--toast-bg);
  color: var(--toast-text);
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  cursor: pointer;
  animation: pc-toast-in 0.18s ease-out;
  transition: opacity 0.2s, transform 0.2s;
}

.toast-success {
  border-left-color: var(--ok);
}

.toast-error {
  border-left-color: var(--danger);
}

.toast-icon {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--focus);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 800;
}

.toast-success .toast-icon {
  background: #3f7f2a;
}

.toast-error .toast-icon {
  background: var(--danger);
}

.toast-msg {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
}

.toast-x {
  flex: none;
  width: 32px;
  min-height: 32px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  font-size: 1.25rem;
  font-weight: 400;
  line-height: 1;
  opacity: 0.7;
}

.toast-out {
  opacity: 0;
  transform: translateY(6px);
}

/* ---------- Online play: identity chip, banners, lobby, host sheet, game cards ---------- */
.identity {
  display: flex;
  flex: 0 1 auto;
  align-items: center;
  gap: 8px;
  min-width: 0;
  margin-left: var(--space-2);
  color: var(--chrome-text);
  font-weight: 600;
}

.identity:empty {
  display: none;
}

.identity-icon {
  display: inline-flex;
  flex: none;
  color: var(--chrome-muted);
}

.identity-icon svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.identity-name {
  min-width: 0;
  max-width: 16ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* A banner's text and its buttons: the buttons drop under the text when the line is narrow. */
.notice {
  flex-wrap: wrap;
  align-items: center;
}

.notice > :first-child {
  flex: 1 1 14rem;
}

.notice-actions {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* Where a game takes the whole screen (no header), the banners that matter in a game float over
   its top edge: the connection, a server restart and "Connection trouble". The selector is more
   specific than game.css's landscape rule, which hides the whole list. */
@media (max-width: 599px), (orientation: landscape) and (max-height: 520px) {
  body.in-game .notices.has-in-game {
    display: grid;
    position: fixed;
    z-index: 30;
    top: env(safe-area-inset-top, 0px);
    left: 0;
    right: 0;
    width: min(100% - 16px, var(--content-w));
    margin: 8px auto 0;
  }

  body.in-game .notices .notice:not(.in-game) {
    display: none;
  }
}

/* A text page such as the privacy policy. */
.prose {
  max-width: 46rem;
  margin-inline: auto;
}

.prose h2 {
  margin-top: var(--space-5);
  font-size: 1.2rem;
}

.prose ul {
  margin: 0 0 var(--space-3);
  padding-left: 1.25em;
}

.prose li + li {
  margin-top: 6px;
}

/* The lobby */
.lobby {
  display: grid;
  gap: var(--space-4);
  max-width: 880px;
  margin: 0 auto;
}

.lobby .page-head {
  margin-bottom: 0;
}

.lobby-greeting {
  margin: 0;
  font-size: 1.05rem;
}

.play-card {
  display: grid;
  gap: var(--space-3);
}

.play-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.play-soon {
  margin: 0;
}

.lobby-section > h2 {
  margin-bottom: var(--space-2);
}

.lobby-body {
  display: grid;
  gap: var(--space-2);
}

.lobby-body > p {
  margin: 0;
}

.room-card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
}

.room-main {
  display: grid;
  flex: 1 1 14rem;
  gap: 4px;
  min-width: 0;
}

.room-main > * {
  margin: 0;
}

.room-name {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 1.05rem;
}

.room-title {
  min-width: 0;
  overflow-wrap: anywhere;
}

.lock {
  display: inline-flex;
  flex: none;
  color: var(--muted);
}

.lock-icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.room-host,
.room-players {
  color: var(--muted);
}

.room-host strong,
.live-side {
  color: var(--text);
}

.live-side {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.room-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.room-actions {
  display: flex;
  flex: none;
  gap: var(--space-2);
}

.room-actions .btn {
  min-width: 96px;
}

/* The host sheet */
.host-form {
  display: grid;
  gap: var(--space-3);
}

.host-form .field {
  margin-bottom: 0;
}

.timer-field {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.timer-field legend {
  padding: 0;
  margin-bottom: 4px;
  font-weight: 600;
}

.timer-opt {
  align-items: flex-start;
  padding: 6px 0;
}

.timer-opt > input {
  flex: none;
  margin-top: 2px;
}

.timer-text {
  display: grid;
  min-width: 0;
}

/* The game view's cards: waiting, starting, closed, gone */
.game-card {
  display: grid;
  gap: var(--space-3);
  max-width: 560px;
  margin: var(--space-5) auto;
}

.game-card > * {
  margin: 0;
}

.game-over-card {
  text-align: center;
}

.game-over-card .btn-row {
  justify-content: center;
}

.waiting-status {
  font-weight: 700;
}

.share-row {
  display: flex;
  gap: var(--space-2);
}

.share-input {
  flex: 1 1 auto;
  min-width: 0;
  font-family: var(--font-mono);
  font-size: 14px;
}

.game-draft {
  display: grid;
  gap: var(--space-3);
}

.game-draft > * {
  margin: 0;
}

.draft-title {
  font-size: 1.1rem;
  text-align: center;
}

.draft-controls {
  justify-content: center;
}

.draft-opponent,
.draft-away-time {
  font-weight: 600;
}

.draft-away-time {
  font-variant-numeric: tabular-nums;     /* the grace countdown ticks every second */
}

@media (max-width: 599px) {
  .identity {
    margin-left: auto;
  }

  .identity-name {
    display: none;                         /* PD §7.6: the greeting in the lobby shows the full name */
  }

  .room-actions {
    flex: 1 1 100%;
  }

  .room-actions .btn {
    flex: 1 1 auto;
  }
}

/* ---------- Animations ---------- */
@keyframes pc-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pc-pop {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

@keyframes pc-sheet-in {
  from { transform: translateY(100%); }
  to { transform: none; }
}

@keyframes pc-toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* ---------- Tablets and up ---------- */
@media (min-width: 600px) {
  .view {
    width: min(100% - 48px, var(--content-w));
    margin-top: var(--space-5);
  }

  .notices {
    width: min(100% - 48px, var(--content-w));
  }
}

/* ---------- Phones (under 600 px) ---------- */
@media (max-width: 599px) {
  .app-header {
    padding: 0 var(--space-3);
  }

  .app-nav {
    display: none;
  }

  .tabbar {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    position: fixed;
    z-index: 20;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--chrome-solid);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
  }

  body {
    padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
  }

  h1 {
    font-size: 1.4rem;
  }

  .hero-logo {
    display: none;
  }

  .hero-title {
    font-size: 1.6rem;
  }

  .big-links {
    grid-template-columns: minmax(0, 1fr);
  }

  .big-link {
    min-height: 76px;
    padding: var(--space-3) var(--space-4);
  }

  .card {
    padding: var(--space-3);
  }

  /* Dialogs become bottom sheets. */
  .dialog,
  .dialog.dialog-sheet {
    width: 100%;
    max-width: 100%;
    max-height: calc(100% - 24px);
    margin: auto 0 0;
    border-radius: 16px 16px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .dialog[open] {
    animation-name: pc-sheet-in;
    animation-duration: 0.2s;
  }

  .dialog-inner {
    padding: 12px 16px 16px;
  }

  /* A grab handle, so it reads as a sheet. */
  .dialog-inner::before {
    content: "";
    align-self: center;
    width: 36px;
    height: 4px;
    margin-bottom: -4px;
    border-radius: 2px;
    background: var(--border);
  }

  .dialog-actions > * {
    flex: 1 1 0;
    min-width: 0;
  }

  .dialog.is-fallback {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    transform: none;
  }

  html.dialog-open,
  html.dialog-open body {
    overflow: hidden;
  }

  .toasts {
    top: calc(var(--space-2) + env(safe-area-inset-top, 0px));
    bottom: auto;
    left: var(--space-3);
    right: var(--space-3);
    width: auto;
    align-items: stretch;
  }

  .footer-links {
    column-gap: var(--space-4);
  }

  /* A game in progress takes the whole screen (views add body.in-game). */
  body.in-game {
    padding-bottom: 0;
  }

  body.in-game .app-header,
  body.in-game .tabbar,
  body.in-game .app-footer,
  body.in-game .notices {
    display: none;
  }

  body.in-game .view {
    width: 100%;
    margin: 0;
  }
}

/* ---------- Motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-delay: 0s !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- M3: accounts (header menu, avatars, the Account and Profile pages) ---------- */
.identity {
  position: relative;
}

.identity-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 0 8px;
  border-color: transparent;
  background: transparent;
  color: var(--chrome-text);
}

.identity-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 30;
  min-width: 11rem;
  margin: 0;
  padding: 6px;
  list-style: none;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: var(--radius);
  background: var(--chrome-solid);
  color: var(--chrome-text);
  box-shadow: var(--shadow-lg);
}

.identity-menu a,
.identity-menu button {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  min-height: var(--tap);
  padding: 8px 12px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.identity-menu a:hover,
.identity-menu button:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.12);
}

.identity-menu button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.login-btn {
  flex: none;
  padding: 6px 12px;
  font-size: 0.9rem;
  white-space: nowrap;
}

.login-short {
  display: none;
}

.avatar {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 50%;
  background: var(--surface-3);
  color: var(--muted);
  vertical-align: middle;
}

.avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar .person-icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.name-node {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow-wrap: anywhere;
}

.player-link {
  color: var(--link);
}

.room-host .avatar {
  margin-right: 6px;
}

.account-view,
.profile-view {
  display: grid;
  gap: var(--space-4);
  max-width: 46rem;
  margin-inline: auto;
}

.account-who {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.account-who p {
  margin: 4px 0 0;
}

.account-name {
  margin: 0;
  font-size: 1.3rem;
  overflow-wrap: anywhere;
}

.account-danger {
  border-color: var(--danger);
}

.profile-games {
  display: grid;
  gap: var(--space-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.profile-game {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
}

.profile-game-main {
  flex: 1 1 auto;
  min-width: 0;
}

.profile-game-main p {
  margin: 0;
  overflow-wrap: anywhere;
}

.pill.result-win {
  background: var(--accent);
  color: var(--accent-text);
}

.pill.result-loss {
  background: var(--danger);
  color: #fff;
}

.import-summary p {
  margin: 0 0 var(--space-2);
}

@media (max-width: 599px) {
  .login-long {
    display: none;
  }

  .login-short {
    display: inline;
  }
}

/* ---------- Moves mode: lobby switch, room chip, help page and sheet (WP-64) ---------- */
.chip-moves-mode {
  background: var(--accent);
  color: var(--accent-text);
}

/* The switch that is off because the other one was turned on: greyed, still a tap away. */
.toggle-row.is-dim .switch {
  opacity: 0.6;
}

.link-btn {
  min-height: var(--tap);
  padding: 0 var(--space-2);
  border: 0;
  background: none;
  color: var(--link);
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.btn-help {
  min-width: var(--tap);
  font-weight: 700;
}

.moves-page {
  max-width: 760px;
  margin: 0 auto;
}

.mv-help {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-width: 0;
  overflow-wrap: anywhere;
}

.mv-help h2 {
  margin: 0 0 var(--space-2);
  font-size: 1.1rem;
}

.mv-help h3 {
  margin: 0 0 var(--space-1);
  font-size: 1rem;
}

.mv-help p {
  margin: 0 0 var(--space-2);
}

.mv-intro {
  font-size: 1.05rem;
}

.mv-how-list {
  display: grid;
  gap: var(--space-2);
  margin: 0;
}

.mv-how-list dt {
  font-weight: 700;
}

.mv-how-list dd {
  margin: 0 0 var(--space-2);
}

.mv-filters {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
}

.mv-filter {
  min-height: var(--tap);
  padding: 0 var(--space-3);
  border: 1px solid var(--border);
  cursor: pointer;
}

.mv-filter[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.mv-dot {
  display: inline-block;
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35);
}

.mv-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.mv-entry {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
}

.mv-entry[hidden] {
  display: none;
}

.mv-entry > summary,
.mv-legend > summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  min-height: var(--tap);
  padding: var(--space-2) var(--space-3);
  cursor: pointer;
}

.mv-type {
  font-weight: 700;
}

.mv-body {
  display: grid;
  gap: var(--space-3);
  padding: 0 var(--space-3) var(--space-3);
}

.mv-legend-list {
  display: grid;
  gap: var(--space-2);
  margin: 0;
  padding: 0 var(--space-3) var(--space-3);
  list-style: none;
}

.mv-legend-list li {
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
}

.mv-badge {
  flex: none;
  min-width: 2.2em;
  padding: 0 6px;
  border-radius: var(--radius-pill);
  background: var(--surface-3);
  font-size: 0.75rem;
  font-weight: 700;
  text-align: center;
}

.mv-badge.tone-bad {
  background: var(--danger);
  color: #fff;
}

.mv-badge.tone-good {
  background: var(--accent);
  color: var(--accent-text);
}

.mv-badge.tone-info {
  background: var(--focus);
  color: #fff;
}

/* On a phone the help sheet fills the screen. */
@media (max-width: 599px) {
  .dialog.dialog-sheet:has(.mv-help) {
    width: 100%;
    max-height: 100%;
    height: 100%;
    margin: 0;
    border-radius: 0;
  }
}
