:root {
  --bg: #d5c1a3;
  --bg-2: #eadcc7;
  --card: #fff8ec;
  --card-2: #f2e5d0;
  --text: #241d18;
  --muted: #7b6d5c;
  --accent: #d30000;
  --accent-2: #0057b8;
  --good: #0057b8;
  --warn: #f2c94c;
  --bad: #d30000;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --timer-warn-bg: #f2c94c;
  --timer-danger-bg: #c90000;
  --timer-overtime-bg: #8c0000;
  --timer-overtime-bg-2: #b40000;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

body { overflow: hidden; }
html.timer-warn,
body.timer-warn { background: var(--timer-warn-bg); }
html.timer-danger,
body.timer-danger { background: var(--timer-danger-bg); }
html.timer-overtime,
body.timer-overtime {
  background: var(--timer-overtime-bg);
  animation: overtime-pulse 1.6s ease-in-out infinite;
}

.screen {
  display: none;
  position: fixed;
  inset: 0;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bot);
}
.screen.active { display: flex; flex-direction: column; }

/* ---------- Setup screen ---------- */
#setup-screen,
#scores-screen {
  width: min(100%, 430px);
  margin: 0 auto;
  padding: calc(var(--safe-top) + 10px) 20px calc(var(--safe-bot) + 10px);
  background: linear-gradient(180deg, var(--bg-2) 0%, var(--bg) 100%);
}

.screen.active#setup-screen,
.screen.active#scores-screen {
  display: flex;
  flex-direction: column;
}

#setup-screen {
  overflow: hidden;
}

#scores-screen {
  overflow-y: auto;
}

.joker-toggle {
  width: 48px;
  height: 48px;
  min-width: 48px;
  max-width: 48px;
  min-height: 48px;
  max-height: 48px;
  flex: 0 0 48px;
  padding: 0;
  border: none;
  border-radius: 50%;
  display: block;
  margin: 0 auto 8px;
  background: var(--bg);
  cursor: pointer;
}
.joker-toggle:active { transform: scale(0.96); }
.joker-toggle .page-joker {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: block;
  object-fit: cover;
}

.block {
  background: var(--card);
  border: 1px solid rgba(36, 29, 24, 0.1);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 8px;
}

.block h2 {
  font-size: 13px;
  font-weight: 600;
  margin: 0 0 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.block h2 .hint {
  font-size: 11px;
  font-weight: 400;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
  opacity: 0.7;
}

/* Seat order circle */
.seat-circle {
  position: relative;
  width: 300px;
  height: 300px;
  margin: 4px auto 0;
  max-width: 100%;
}
.seat-arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}
.seat {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.seat-core {
  position: absolute;
  width: 76px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-align: center;
  transform: translate(-50%, 0);
}
.seat-core .avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  object-fit: cover;
}
.seat-core .name {
  font-size: 12px;
  font-weight: 600;
  line-height: 1.1;
}
.seat .controls {
  position: absolute;
  display: flex;
  gap: 4px;
  pointer-events: auto;
}
.seat .controls.vertical {
  flex-direction: column;
}
.seat .controls button {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: rgba(36, 29, 24, 0.08);
  border: none;
  color: var(--text);
  font-size: 14px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  font-family: inherit;
}
.seat .controls button:active { background: rgba(36, 29, 24, 0.16); }
.seat .controls button:disabled {
  color: rgba(36, 29, 24, 0.3);
  cursor: not-allowed;
}
.seat .controls .remove {
  background: rgba(211, 0, 0, 0.12);
  color: #b40000;
}
.seat .controls .remove:active { background: rgba(211, 0, 0, 0.22); }

/* Bench (sitting out) */
.bench {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(36, 29, 24, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}
.bench[hidden] {
  display: none;
}
.bench .label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  white-space: nowrap;
}
.bench-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}
.bench-player {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 3px 4px 3px 3px;
  border-radius: 18px;
  background: rgba(36, 29, 24, 0.06);
}
.bench-player .avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  opacity: 0.55;
}
.bench-player .name {
  font-size: 12px;
  color: var(--muted);
}
.bench-player .add {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--good);
  color: #fff8ec;
  border: none;
  font-size: 17px;
  font-weight: 700;
  line-height: 1;
  padding: 0 0 2px;
  cursor: pointer;
}
.bench-player .add:active { transform: scale(0.95); }

/* First-player picker */
.first-picker {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}
.first-option {
  display: flex;
  align-items: center;
  gap: 5px;
  min-height: 36px;
  padding: 3px 8px 3px 3px;
  border-radius: 18px;
  background: var(--card-2);
  border: none;
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
}
.first-option .avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
}
.first-option .name {
  font-size: 12px;
  font-weight: 600;
}
.first-option.selected {
  background: rgba(211, 0, 0, 0.08);
  box-shadow: 0 0 0 2px var(--accent) inset;
}
.first-option:active { transform: scale(0.97); }

.link-btn {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.6px;
  padding: 4px 6px;
  text-transform: uppercase;
  border-radius: 6px;
  margin: -4px -6px;
}
.link-btn:active { background: rgba(211, 0, 0, 0.1); }
.reorder button {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: none;
  background: rgba(36,29,24,0.08);
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
}
.reorder button:disabled { opacity: 0.25; }
.reorder button:active:not(:disabled) { background: rgba(36,29,24,0.16); }

/* Presets */
.preset-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.duration-block .preset-row {
  width: 90%;
  margin: 0 auto;
}
.preset, .preset-input {
  display: block;
  width: 100%;
  border: none;
  background: var(--card-2);
  color: var(--text);
  padding: 13px 4px;
  border-radius: 11px;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  font-family: inherit;
  min-width: 0;
  margin: 0;
}
.duration-block .preset,
.duration-block .preset-input {
  padding: 10px 4px;
  border-radius: 9px;
  font-size: 14px;
}
.preset { cursor: pointer; }
.preset.active,
.preset-input.active {
  background: var(--accent);
  color: #fff8ec;
}
.preset:active { transform: scale(0.97); }
.preset-input {
  -webkit-appearance: none;
  -moz-appearance: textfield;
  appearance: none;
}
.preset-input::-webkit-outer-spin-button,
.preset-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.preset-input::placeholder { color: var(--muted); opacity: 1; font-weight: 600; }
.preset-input:focus { outline: 2px solid var(--accent); background: var(--bg-2); }
.preset-input.active:focus {
  background: var(--accent);
  color: #fff8ec;
}

/* Scores */
.scores-heading {
  text-align: center;
  margin: 0 0 10px;
}
.scores-heading h1 {
  font-size: 26px;
  line-height: 1;
  margin: 0 0 4px;
  letter-spacing: 0;
}
.scores-meta {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
}
.score-block {
  padding: 12px;
}
.score-status {
  margin: 0 0 8px;
  padding: 9px 10px;
  border-radius: 8px;
  background: rgba(36, 29, 24, 0.07);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  text-align: center;
}
.score-status.error {
  background: rgba(211, 0, 0, 0.1);
  color: #9b0000;
}
.score-table-wrap {
  overflow-x: auto;
}
.score-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}
.score-table th,
.score-table td {
  border-bottom: 1px solid rgba(36, 29, 24, 0.1);
  padding: 8px 5px;
  color: var(--text);
  font-size: 12px;
  line-height: 1.15;
  vertical-align: middle;
}
.score-table th {
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}
.score-table th:first-child,
.score-table td:first-child {
  width: 36%;
  text-align: left;
}
.total-score-table th:first-child,
.total-score-table td:first-child {
  width: 56%;
}
.score-table th:not(:first-child),
.score-table td:not(:first-child) {
  text-align: right;
}
.score-table tbody tr:last-child td {
  border-bottom: none;
}
.score-player {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 7px;
}
.score-player .avatar {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  border-radius: 50%;
  object-fit: cover;
}
.score-player .name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  font-weight: 750;
}
.score-table .numeric {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.primary-btn {
  width: 100%;
  padding: 12px;
  border-radius: 14px;
  border: none;
  background: var(--accent);
  color: #fff8ec;
  font-size: 17px;
  font-weight: 800;
  cursor: pointer;
  margin-top: 0;
  letter-spacing: 0.3px;
}
.primary-btn:disabled {
  background: var(--card-2);
  color: var(--muted);
  cursor: not-allowed;
}
.primary-btn:active:not(:disabled) { transform: scale(0.98); }

@media (max-height: 760px) {
  #setup-screen {
    padding: calc(var(--safe-top) + 12px) 18px calc(var(--safe-bot) + 10px);
  }

  .block {
    padding: 14px;
    margin-bottom: 10px;
  }

  .block h2 {
    margin-bottom: 8px;
  }

  .primary-btn {
    padding: 15px;
  }
}

/* ---------- Timer screen ---------- */
#timer-screen {
  background: var(--bg);
  transition: background-color 0.4s;
}
#timer-screen.warn { background: var(--timer-warn-bg); }
#timer-screen.danger {
  background: var(--timer-danger-bg);
  color: #fff8ec;
}
#timer-screen.overtime {
  background: var(--timer-overtime-bg);
  color: #fff8ec;
  animation: overtime-pulse 1.6s ease-in-out infinite;
}
@keyframes overtime-pulse {
  0%, 100% { background-color: var(--timer-overtime-bg); }
  50% { background-color: var(--timer-overtime-bg-2); }
}

.back-btn {
  position: absolute;
  top: calc(var(--safe-top) + 8px);
  left: 10px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: rgba(36,29,24,0.08);
  color: var(--text);
  font-size: 28px;
  line-height: 1;
  padding: 0 0 4px;
  cursor: pointer;
  z-index: 10;
}
.back-btn:active { background: rgba(36,29,24,0.16); }

.timer-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
  cursor: pointer;
  position: relative;
}

.current-player {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.avatar-large {
  width: min(60vw, 240px);
  height: min(60vw, 240px);
  border-radius: 50%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
  object-fit: cover;
}
.avatar-large.joker {
  animation: joker-pulse 0.85s ease-in-out infinite;
}
@keyframes joker-pulse {
  0%, 100% { transform: scale(0.85); }
  50%      { transform: scale(1.05); }
}

.current-name {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.time-display {
  font-size: clamp(64px, 18vw, 120px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: -2px;
  margin-top: 12px;
  font-feature-settings: "tnum";
}
