/* Base: reset, type, browser surfaces, controls and shared primitives. */

html.hosted-entry-route .app {
  display: none !important;
}

html.hosted-entry-route #hostedEntry[hidden] {
  display: block !important;
}

html.hosted-game-pending .app {
  visibility: hidden;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--ground);
  scrollbar-color: var(--rule-strong) transparent;
}

body {
  margin: 0;
  min-height: calc(100 * var(--vh));
  min-height: calc(100 * var(--dvh));
  background: var(--ground);
  color: var(--ink);
  font: 400 var(--text-md) / 1.45 var(--font-ui);
  font-feature-settings: "kern", "ss01";
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

h1,
h2,
h3,
h4,
p {
  margin: 0;
}

h1,
h2,
h3 {
  font-weight: 650;
  letter-spacing: -0.015em;
  text-wrap: balance;
}

small {
  font-size: inherit;
}

strong,
b {
  font-weight: 600;
}

code,
kbd {
  font-family: var(--font-mono);
}

img {
  display: block;
  max-width: 100%;
}

::selection {
  background: var(--accent);
  color: var(--accent-ink);
}

::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-thumb {
  background: var(--rule-strong);
  border: 3px solid transparent;
  border-radius: var(--r-pill);
  background-clip: padding-box;
}

::-webkit-scrollbar-track {
  background: transparent;
}

:focus {
  outline: none;
}

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

[hidden] {
  display: none !important;
}

.screen-reader {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.num,
.timer,
input[type="number"],
output {
  font-variant-numeric: tabular-nums;
}

/* Icons */
.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon.filled {
  fill: currentColor;
  stroke: none;
}

.suit {
  width: 1em;
  height: 1em;
  flex: none;
  fill: currentColor;
}

.suit.red {
  color: var(--red);
}

/* Buttons */
button {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  min-height: 40px;
  padding: 0 var(--s-4);
  border: 1px solid var(--rule-control);
  border-radius: var(--r-sm);
  background: var(--face);
  color: var(--ink);
  font: 550 var(--text-md) / 1 var(--font-ui);
  letter-spacing: -0.005em;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition:
    background-color var(--t-fast) var(--ease-out),
    border-color var(--t-fast) var(--ease-out),
    color var(--t-fast) var(--ease-out),
    box-shadow var(--t-fast) var(--ease-out),
    transform var(--t-fast) var(--ease-out);
}

button:hover:not(:disabled) {
  background: var(--face-2);
  border-color: var(--rule-strong);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  color: var(--ink-3);
  background: transparent;
  border-color: var(--rule);
}

button.primary {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--ink-inverse);
}

button.primary:hover:not(:disabled) {
  background: color-mix(in srgb, var(--ink) 86%, var(--ground));
  border-color: color-mix(in srgb, var(--ink) 86%, var(--ground));
}

button.primary:disabled {
  background: var(--face-3);
  border-color: var(--face-3);
  color: var(--ink-3);
}

button.danger {
  color: var(--red);
  border-color: var(--red-line);
  background: transparent;
}

button.danger:hover:not(:disabled) {
  background: var(--red-soft);
  border-color: var(--red);
}

button.ghost {
  border-color: transparent;
  background: transparent;
  color: var(--ink-2);
}

button.ghost:hover:not(:disabled) {
  background: var(--face-3);
  border-color: transparent;
  color: var(--ink);
}

.small-button {
  min-height: 32px;
  padding: 0 var(--s-3);
  font-size: var(--text-sm);
  border-radius: var(--r-xs);
}

.icon-button {
  width: 44px;
  min-width: 44px;
  padding: 0;
}

/* Fields */
label {
  display: grid;
  gap: 6px;
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: 500;
}

label:not(:has(input, select, textarea)) {
  display: block;
}

label small {
  color: var(--ink-3);
  font-weight: 400;
}

input,
select,
textarea {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  margin: 0;
  padding: 0 var(--s-3);
  border: 1px solid var(--rule-control);
  border-radius: var(--r-sm);
  background: var(--face);
  color: var(--ink);
  font: 450 var(--text-md) / 1.2 var(--font-ui);
  caret-color: var(--accent);
  transition: border-color var(--t-fast) var(--ease-out), box-shadow var(--t-fast) var(--ease-out);
}

input::placeholder,
textarea::placeholder {
  color: var(--ink-3);
  opacity: 1;
}

input:hover:not(:disabled),
select:hover:not(:disabled),
textarea:hover:not(:disabled) {
  border-color: var(--ink-3);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input:disabled,
select:disabled,
textarea:disabled {
  color: var(--ink-3);
  background: var(--face-2);
  border-color: var(--rule);
  cursor: not-allowed;
}

select {
  appearance: none;
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1.5 1.75 6 6.25l4.5-4.5' fill='none' stroke='%234b4f59' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 13px center;
  cursor: pointer;
}

:root[data-theme-mode="dark"] select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1.5 1.75 6 6.25l4.5-4.5' fill='none' stroke='%23bcc2cd' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

textarea {
  min-height: 280px;
  padding: var(--s-3);
  font: 400 var(--text-sm) / 1.5 var(--font-mono);
  resize: vertical;
}

input[type="number"] {
  -moz-appearance: textfield;
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="color"] {
  padding: 0;
  cursor: pointer;
}

/* Range: a single rule with a filled run and a card-edge thumb */
input[type="range"] {
  --range-progress: 50%;
  appearance: none;
  height: 28px;
  min-height: 28px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

input[type="range"]:focus {
  box-shadow: none;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: var(--r-pill);
  background: linear-gradient(90deg, var(--accent) 0 var(--range-progress), var(--rule-strong) var(--range-progress) 100%);
}

input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: var(--r-pill);
  background: var(--rule-strong);
}

input[type="range"]::-moz-range-progress {
  height: 4px;
  border-radius: var(--r-pill);
  background: var(--accent);
}

input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 20px;
  height: 20px;
  margin-top: -8px;
  border: 1px solid var(--rule-control);
  border-radius: 50%;
  background: var(--face);
  box-shadow: var(--lift-1);
  transition: transform var(--t-fast) var(--ease-out);
}

input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border: 1px solid var(--rule-control);
  border-radius: 50%;
  background: var(--face);
  box-shadow: var(--lift-1);
}

input[type="range"]:hover::-webkit-slider-thumb {
  transform: scale(1.08);
}

input[type="range"]:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 0 0 4px var(--accent-soft), var(--lift-1);
  border-color: var(--accent);
}

input[type="range"]:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

/* Disclosure */
details > summary {
  list-style: none;
  cursor: pointer;
}

details > summary::-webkit-details-marker {
  display: none;
}

/* Status chips: text first, colour second */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--ink-2);
  font: 550 var(--text-sm) / 1.2 var(--font-ui);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.badge.red {
  color: var(--red);
}

/* Faces */
.panel {
  background: var(--face);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-4);
  padding: var(--s-5) var(--s-6) 0;
}

.panel-header h2 {
  font-size: var(--text-xl);
}

.panel-body {
  padding: var(--s-5) var(--s-6) var(--s-6);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
}

.stat-box {
  display: grid;
  gap: 4px;
  align-content: start;
}

.stat-box > span {
  color: var(--ink-3);
  font-size: var(--text-sm);
}

.stat-box > strong {
  font: 500 var(--text-2xl) / 1.15 var(--font-display);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 90;
  max-width: min(480px, calc(calc(100 * var(--vw)) - 32px));
  padding: var(--s-3) var(--s-5);
  border-radius: var(--r-md);
  background: var(--ink);
  color: var(--ink-inverse);
  font-size: var(--text-md);
  font-weight: 500;
  box-shadow: var(--lift-3);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity var(--t-base) var(--ease-out), transform var(--t-base) var(--ease-out);
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Dialogs */
.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: var(--s-4);
  background: var(--scrim);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--t-base) var(--ease-out), visibility 0s linear var(--t-base);
}

.modal.open {
  opacity: 1;
  visibility: visible;
  transition: opacity var(--t-base) var(--ease-out), visibility 0s;
}

.modal-card {
  width: min(560px, 100%);
  max-height: calc(calc(100 * var(--dvh)) - 32px);
  overflow: auto;
  background: var(--face);
  border: 1px solid var(--rule);
  border-radius: var(--r-card);
  box-shadow: var(--lift-3);
  transform: translateY(10px) scale(0.985);
  transition: transform 260ms var(--ease-out);
}

.modal.open .modal-card {
  transform: none;
}

#jsonModal .modal-card {
  width: min(760px, 100%);
}

.dialog-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: start;
  gap: var(--s-5) var(--s-4);
  padding: var(--s-8);
}

.dialog-card > :not(.dialog-mark, .dialog-copy) {
  grid-column: 1 / -1;
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-4);
}

.dialog-head h2 {
  font-size: var(--text-2xl);
}

.dialog-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  margin-top: 2px;
  border-radius: 50%;
  background: var(--face-3);
  color: var(--ink-2);
}

.dialog-mark .icon {
  width: 22px;
  height: 22px;
}

.dialog-mark.danger {
  background: var(--red-soft);
  color: var(--red);
}

.dialog-copy {
  display: grid;
  gap: var(--s-2);
}

.dialog-copy h2 {
  font-size: var(--text-2xl);
}

.dialog-copy p {
  color: var(--ink-2);
  line-height: 1.5;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--s-2);
  flex-wrap: wrap;
}

.dialog-field {
  gap: var(--s-2);
}

.dialog-field input {
  min-height: 48px;
  font-size: var(--text-lg);
}

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

@media (pointer: coarse) {
  button {
    min-height: 44px;
  }

  input,
  select {
    min-height: 44px;
    font-size: 16px;
  }
}
