/* ---------- Page setup and classroom-friendly colors ---------- */
:root {
  --bg: #f4f7fb;
  --card: #ffffff;
  --ink: #183153;
  --muted: #5c6f86;
  --blue: #2364aa;
  --blue-dark: #174b85;
  --green: #2f855a;
  --yellow: #f9c74f;
  --orange: #f97316;
  --red: #c2410c;
  --border: #d7e1ef;
  --panel: #f8fbff;
  --top-pill: #e0f2fe;
  --top-pill-ink: #174b85;
  --badge-bg: #fff7cc;
  --badge-ink: #7c4a03;
  --die-bg: #ffffff;
  --routine-bg: #ffffff;
  --deck-start: #dbeafe;
  --hint-bg: #eefdf4;
  --hint-border: #bbf7d0;
  --hint-card-bg: #ffffff;
  --hint-text: #27563b;
  --score-bg: #fff7ed;
  --score-border: #fed7aa;
  --settings-bg: rgba(255, 255, 255, 0.98);
  --history-item-bg: #f8fbff;
  --shadow: 0 14px 34px rgba(24, 49, 83, 0.14);
  --radius: 20px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Arial, Helvetica, sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at top left, #dff3ff 0, transparent 32%), var(--bg);
  font-size: 17px;
}

body.dark-mode {
  --bg: #0f172a;
  --card: #172033;
  --ink: #f8fafc;
  --muted: #b7c5d9;
  --blue: #3b82f6;
  --blue-dark: #1d4ed8;
  --green: #7dd3a7;
  --yellow: #facc15;
  --orange: #fb923c;
  --red: #c2410c;
  --border: #334155;
  --panel: #1e293b;
  --top-pill: #1e3a5f;
  --top-pill-ink: #bfdbfe;
  --badge-bg: #43320c;
  --badge-ink: #fde68a;
  --die-bg: #111827;
  --routine-bg: #111827;
  --deck-start: #1e40af;
  --hint-bg: #102a27;
  --hint-border: #1f6b50;
  --hint-card-bg: #172033;
  --hint-text: #bbf7d0;
  --score-bg: #2a2118;
  --score-border: #9a5b22;
  --settings-bg: rgba(23, 32, 51, 0.99);
  --history-item-bg: #1e293b;
  --shadow: 0 18px 36px rgba(0, 0, 0, 0.35);
  background: radial-gradient(circle at top left, rgba(59, 130, 246, 0.22) 0, transparent 34%), var(--bg);
}

button,
input {
  font: inherit;
}

button {
  min-height: 46px;
  border: 0;
  border-radius: 14px;
  padding: 0.65rem 0.95rem;
  color: #ffffff;
  background: var(--blue);
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 5px 0 var(--blue-dark);
  touch-action: manipulation;
}

button:hover:not(:disabled),
button:focus-visible:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.05);
}

button:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 2px 0 var(--blue-dark);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  box-shadow: none;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

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

/* ---------- Compact desktop-first layout ---------- */
.top-bar {
  position: relative;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  width: min(100% - 2rem, 1760px);
  margin: 0 auto;
  padding: 0.7rem 0 0.55rem;
}

.top-left,
.top-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.app-title {
  font-size: clamp(1.2rem, 1.6vw, 1.65rem);
  letter-spacing: -0.02em;
}

.turn-pill,
.history-heading span {
  display: inline-flex;
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  background: var(--top-pill);
  color: var(--top-pill-ink);
  font-weight: 800;
  white-space: nowrap;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(300px, 370px);
  gap: 1rem;
  width: min(100% - 2rem, 1760px);
  height: calc(100vh - 76px);
  min-height: 690px;
  margin: 0 auto;
  padding-bottom: 0.9rem;
}

.game-card,
.history-panel {
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.game-card {
  display: grid;
  grid-template-rows: auto auto auto;
  gap: 0.8rem;
  min-height: 0;
  padding: clamp(0.85rem, 1.4vw, 1.25rem);
}

/* ---------- Main result area ---------- */
.result-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.85rem;
  min-height: 0;
}

.result-panel {
  position: relative;
  min-height: 260px;
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: clamp(0.85rem, 1.2vw, 1.15rem);
  background: var(--panel);
  text-align: center;
  overflow: hidden;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.result-panel.active-step {
  border-color: var(--orange);
  box-shadow: 0 0 0 5px rgba(249, 115, 22, 0.18), 0 16px 34px rgba(249, 115, 22, 0.16);
  transform: translateY(-2px);
}

.result-panel.active-step::after {
  content: "Now!";
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  color: #ffffff;
  background: var(--orange);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.label {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.time-display {
  margin-top: 0.55rem;
}

.big-result {
  display: block;
  font-size: clamp(3.3rem, 5.7vw, 6.4rem);
  font-weight: 900;
  line-height: 0.95;
}

.ampm-badge,
.medium-result {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 84px;
  margin-top: 0.5rem;
  border-radius: 14px;
  padding: 0.35rem 0.65rem;
  background: var(--badge-bg);
  color: var(--badge-ink);
  font-size: clamp(1.15rem, 1.5vw, 1.5rem);
  font-weight: 900;
}

.helper-text {
  margin: 0.45rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.dice-display {
  display: flex;
  justify-content: center;
  gap: 0.45rem;
  margin-top: 0.6rem;
}

.die {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  background: var(--die-bg);
  font-size: 1.5rem;
  font-weight: 900;
}

.second-die.is-hidden {
  display: none;
}

.coin {
  display: grid;
  place-items: center;
  width: clamp(104px, 7vw, 132px);
  height: clamp(104px, 7vw, 132px);
  margin: 0.8rem auto 0.6rem;
  border: 8px solid #d99a00;
  border-radius: 50%;
  background: var(--yellow);
  color: #5f3b00;
  font-size: clamp(1.8rem, 2.2vw, 2.4rem);
  font-weight: 900;
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.45), 0 10px 20px rgba(0, 0, 0, 0.12);
}

.deck {
  position: relative;
  width: 90px;
  height: 58px;
  margin: 0.55rem auto;
}

.deck span {
  position: absolute;
  inset: 0;
  border: 3px solid var(--blue-dark);
  border-radius: 10px;
  background: linear-gradient(135deg, var(--deck-start), var(--routine-bg));
}

.deck span:nth-child(1) { transform: rotate(-8deg); }
.deck span:nth-child(2) { transform: rotate(3deg) translate(4px, 1px); }
.deck span:nth-child(3) { transform: rotate(10deg) translate(8px, 2px); }

.routine-card {
  display: grid;
  align-content: center;
  min-height: 126px;
  border: 3px solid var(--blue);
  border-radius: 18px;
  padding: 0.75rem;
  background: var(--routine-bg);
}

.card-number,
.points-line {
  display: block;
  color: var(--blue-dark);
  font-weight: 900;
}

.routine-card strong {
  display: block;
  margin: 0.35rem 0;
  font-size: clamp(1.35rem, 2.2vw, 2.25rem);
}

/* ---------- Hints, score, and controls ---------- */
.practice-row {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(260px, 0.55fr);
  gap: 0.8rem;
}

.sentence-hints,
.score-board > div {
  border-radius: 18px;
}

.sentence-hints {
  padding: 0.85rem 1rem;
  background: var(--hint-bg);
  border: 2px solid var(--hint-border);
}

.sentence-hints.is-hidden {
  display: none;
}

.sentence-hints h2,
.history-heading h2 {
  margin: 0;
  font-size: 1.25rem;
}

.sentence-hints ul {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.55rem;
  margin: 0.55rem 0;
  padding: 0;
  list-style: none;
  font-size: clamp(1rem, 1.25vw, 1.25rem);
  font-weight: 800;
}

.sentence-hints li {
  min-width: 0;
}

.hint-instruction {
  margin: 0.35rem 0 0;
}

.hint-card {
  display: grid;
  justify-items: start;
  gap: 0.28rem;
  width: 100%;
  min-height: 96px;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 0.55rem;
  color: var(--ink);
  background: var(--hint-card-bg);
  box-shadow: none;
  text-align: left;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease, background-color 0.18s ease;
}

.hint-card:hover:not(:disabled),
.hint-card:focus-visible:not(:disabled) {
  border-color: var(--green);
  box-shadow: 0 8px 18px rgba(47, 133, 90, 0.18);
  transform: translateY(-2px);
  filter: none;
}

.hint-card:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: none;
}

.hint-label {
  display: inline-flex;
  border-radius: 999px;
  padding: 0.14rem 0.5rem;
  color: #ffffff;
  background: var(--green);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.hint-sentence {
  color: var(--hint-text);
  line-height: 1.25;
}

.hint-phrase {
  display: inline-block;
  filter: blur(6px);
  opacity: 0.55;
  user-select: none;
  transition: filter 0.18s ease, opacity 0.18s ease;
}

.hint-card-phrase {
  min-width: 7.5ch;
}

.hint-card.is-revealed {
  border-color: var(--green);
}

.hint-card.is-revealed .hint-phrase {
  filter: none;
  opacity: 1;
  user-select: text;
}

.sentence-hints p {
  margin: 0;
  color: var(--hint-text);
  font-size: 0.95rem;
}

.score-board {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.7rem;
}

.score-board > div {
  position: relative;
  display: grid;
  align-content: center;
  min-height: 92px;
  padding: 0.75rem;
  background: var(--score-bg);
  border: 2px solid var(--score-border);
  text-align: center;
}

.score-board strong {
  display: block;
  font-size: clamp(1.45rem, 2.3vw, 2.35rem);
}

.score-pop {
  position: absolute;
  right: 1rem;
  top: -0.4rem;
  color: var(--green);
  font-size: 1.5rem;
  font-weight: 900;
  opacity: 0;
}

.button-area {
  display: grid;
  grid-template-columns: minmax(220px, 0.55fr) minmax(0, 1fr);
  gap: 0.75rem;
  align-items: stretch;
}

.primary-button {
  min-height: 64px;
  background: var(--orange);
  box-shadow: 0 6px 0 var(--red);
  font-size: clamp(1.45rem, 2.1vw, 2rem);
}

.manual-controls {
  display: grid;
  gap: 0.45rem;
}

.small-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.65rem;
}

.add-points-button {
  background: var(--green);
  box-shadow: 0 5px 0 #276749;
}

.manual-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
  text-align: center;
}

.reset-button {
  min-height: 42px;
  background: #64748b;
  box-shadow: 0 4px 0 #475569;
}

/* ---------- Settings dropdown and history ---------- */
.settings-menu {
  position: relative;
}

.settings-button {
  min-height: 42px;
  background: var(--blue);
}

.settings-dropdown {
  position: absolute;
  top: calc(100% + 0.55rem);
  right: 0;
  width: min(360px, calc(100vw - 2rem));
  max-height: calc(100vh - 5rem);
  overflow: auto;
  padding: 0.8rem;
  border: 2px solid var(--border);
  border-radius: 18px;
  background: var(--settings-bg);
  box-shadow: var(--shadow);
}

.settings-form {
  display: grid;
  gap: 0.6rem;
}

fieldset {
  display: grid;
  gap: 0.3rem;
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 0.65rem;
}

legend {
  color: var(--blue-dark);
  font-weight: 900;
}

.settings-form label {
  display: flex;
  gap: 0.45rem;
  align-items: center;
  min-height: 30px;
}

.history-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  padding: 1rem;
}

.history-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.history-list {
  display: grid;
  align-content: start;
  gap: 0.55rem;
  min-height: 0;
  margin: 0.8rem 0 0;
  padding-left: 1.4rem;
  overflow: auto;
}

.history-list li {
  border-left: 5px solid var(--blue);
  border-radius: 10px;
  padding: 0.55rem;
  background: var(--history-item-bg);
  font-weight: 700;
}

.empty-history {
  color: var(--muted);
  font-weight: 400;
}

/* ---------- Required animation classes ---------- */
.rolling {
  animation: bounce-roll 0.2s linear infinite;
}

.flipping {
  animation: coin-flip 0.2s linear infinite;
}

.shuffling span:nth-child(1) { animation: shuffle-left 0.26s ease-in-out infinite alternate; }
.shuffling span:nth-child(2) { animation: shuffle-right 0.28s ease-in-out infinite alternate; }
.shuffling span:nth-child(3) { animation: shuffle-up 0.24s ease-in-out infinite alternate; }

.revealing {
  animation: reveal-card 0.45s ease-out;
}

.score-pop.show {
  animation: score-pop 0.85s ease-out;
}

.history-list li.new-history {
  animation: history-in 0.45s ease-out;
}

@keyframes bounce-roll {
  0% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-5px) rotate(2deg); }
  100% { transform: translateY(0) rotate(-2deg); }
}

@keyframes coin-flip {
  from { transform: rotateY(0deg) scale(1); }
  to { transform: rotateY(180deg) scale(1.05); }
}

@keyframes shuffle-left {
  to { transform: translateX(-12px) rotate(-14deg); }
}

@keyframes shuffle-right {
  to { transform: translateX(12px) rotate(12deg); }
}

@keyframes shuffle-up {
  to { transform: translateY(-8px) rotate(4deg); }
}

@keyframes reveal-card {
  from { transform: translateY(14px) scale(0.96); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes score-pop {
  0% { transform: translateY(10px); opacity: 0; }
  25% { transform: translateY(0); opacity: 1; }
  100% { transform: translateY(-28px); opacity: 0; }
}

@keyframes history-in {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ---------- Responsive and reduced-motion support ---------- */
@media (max-width: 1150px) {
  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 0;
  }

  .history-panel {
    max-height: 360px;
  }
}

@media (max-width: 900px) {
  .result-grid,
  .practice-row,
  .button-area {
    grid-template-columns: 1fr;
  }

  .result-panel {
    min-height: auto;
  }

  .sentence-hints ul,
  .small-buttons {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  body {
    font-size: 16px;
  }

  .top-bar,
  .top-left,
  .top-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  button {
    width: 100%;
  }

  .settings-dropdown {
    left: 0;
    right: auto;
  }
}

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

/* ---------- Round finish, clickable deck, modals, and compact history ---------- */
.deck-button {
  display: grid;
  place-items: center;
  width: auto;
  min-height: 0;
  margin: 0.55rem auto;
  padding: 0.1rem;
  color: inherit;
  background: transparent;
  box-shadow: none;
}

.deck-button:hover:not(:disabled),
.deck-button:focus-visible:not(:disabled) {
  transform: translateY(-1px) scale(1.03);
  filter: none;
  outline: 3px solid rgba(35, 100, 170, 0.35);
  outline-offset: 4px;
}

.deck-button:active:not(:disabled) {
  transform: translateY(0) scale(1);
  box-shadow: none;
}

.deck-button .deck {
  margin: 0;
}

.finished-panel {
  display: grid;
  gap: 0.25rem;
  border: 3px solid var(--green);
  border-radius: 18px;
  padding: 0.75rem 1rem;
  background: var(--hint-bg);
  color: var(--hint-text);
  text-align: center;
}

.finished-panel[hidden] {
  display: none;
}

.finished-panel strong {
  color: var(--green);
  font-size: clamp(1.35rem, 2vw, 2rem);
}

.finished-panel span {
  color: var(--ink);
  font-size: 1.2rem;
  font-weight: 900;
}

.finished-panel p {
  margin: 0;
  font-weight: 800;
}

.history-panel {
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 0.65rem;
}

.history-heading {
  width: 100%;
  min-height: 0;
  padding: 0;
  color: var(--ink);
  background: transparent;
  box-shadow: none;
  text-align: left;
}

.history-heading:hover:not(:disabled),
.history-heading:focus-visible:not(:disabled) {
  transform: none;
  filter: none;
}

.history-title-wrap {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0;
  background: transparent;
  color: var(--ink);
}

#history-title {
  font-size: 1.25rem;
}

#historyToggleText {
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  color: #ffffff;
  background: var(--blue);
  font-size: 0.9rem;
  font-weight: 900;
}

.latest-history {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 800;
}

.history-body {
  min-height: 0;
  overflow: hidden;
  animation: history-expand 0.2s ease-out;
}

.history-panel.is-collapsed .history-body {
  display: none;
}

.history-panel.is-collapsed {
  align-content: start;
}

.history-panel.is-expanded .history-body {
  display: block;
}

.schedule-button {
  min-height: 44px;
  background: var(--green);
  box-shadow: 0 4px 0 #276749;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(15, 23, 42, 0.58);
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-card {
  width: min(640px, 100%);
  max-height: min(760px, calc(100vh - 2rem));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border: 2px solid var(--border);
  border-radius: 20px;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow);
  overflow: hidden;
}

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

.modal-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem;
  border-bottom: 2px solid var(--border);
  background: var(--panel);
}

.modal-heading h2 {
  margin: 0;
  font-size: 1.35rem;
}

.modal-close {
  display: grid;
  place-items: center;
  width: 42px;
  min-height: 42px;
  padding: 0;
  border-radius: 50%;
  background: #64748b;
  box-shadow: 0 3px 0 #475569;
  font-size: 1.6rem;
  line-height: 1;
}

.modal-content {
  min-height: 0;
  overflow: auto;
  padding: 1rem;
}

.modal-content h3 {
  margin: 0.25rem 0 0.5rem;
  color: var(--blue-dark);
}

.card-mini-list,
.schedule-list {
  display: grid;
  gap: 0.4rem;
  margin: 0 0 0.9rem;
  padding-left: 1.35rem;
}

.card-mini-list li,
.schedule-list li {
  border-left: 4px solid var(--blue);
  border-radius: 9px;
  padding: 0.45rem 0.55rem;
  background: var(--history-item-bg);
  font-weight: 800;
}

.modal-note {
  margin: 0 0 0.8rem;
  color: var(--muted);
  font-weight: 800;
}

body.demo-mode .result-panel {
  min-height: 315px;
}

body.demo-mode .big-result {
  font-size: clamp(4.4rem, 7.2vw, 8.1rem);
}

body.demo-mode .coin {
  width: clamp(130px, 9vw, 168px);
  height: clamp(130px, 9vw, 168px);
  font-size: clamp(2.25rem, 3vw, 3rem);
}

body.demo-mode .routine-card {
  min-height: 168px;
}

body.demo-mode .routine-card strong {
  font-size: clamp(1.8rem, 3vw, 3rem);
}

body.demo-mode .score-board strong,
body.demo-mode .points-line {
  font-size: clamp(1.8rem, 2.8vw, 2.9rem);
}

@keyframes history-expand {
  from { max-height: 0; opacity: 0; }
  to { max-height: 520px; opacity: 1; }
}

/* Keep Teacher Demo Mode readable without crowding the controls. */
body.demo-mode .game-card {
  gap: 0.65rem;
}

body.demo-mode .button-area {
  grid-template-columns: minmax(170px, 0.34fr) minmax(0, 1fr);
  gap: 0.65rem;
  align-items: start;
}

body.demo-mode .primary-button {
  min-height: 54px;
  padding: 0.55rem 0.8rem;
  font-size: clamp(1.1rem, 1.35vw, 1.4rem);
}

body.demo-mode .small-buttons {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
}

body.demo-mode .small-buttons button {
  min-height: 44px;
  padding: 0.5rem 0.65rem;
  font-size: clamp(0.9rem, 1vw, 1.05rem);
  line-height: 1.15;
  white-space: normal;
}

/* ---------- Manual multi-screen interaction updates ---------- */
.app-view[hidden] {
  display: none !important;
}

.clickable-panel {
  cursor: pointer;
  outline: none;
}

.clickable-panel::before {
  content: "Click to open";
  position: absolute;
  left: 0.8rem;
  bottom: 0.65rem;
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  background: rgba(35, 100, 170, 0.12);
  color: var(--blue-dark);
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.clickable-panel:hover,
.clickable-panel:focus-visible {
  border-color: var(--blue);
  box-shadow: 0 0 0 5px rgba(35, 100, 170, 0.14), var(--shadow);
  transform: translateY(-2px);
}

.clickable-panel.is-disabled {
  cursor: not-allowed;
  opacity: 0.72;
}

.status-pill {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  padding: 0.22rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.status-pill.incomplete {
  background: #fee2e2;
  color: #991b1b;
}

.status-pill.done {
  background: #dcfce7;
  color: #166534;
}

body.dark-mode .status-pill.incomplete {
  background: #4a1d1d;
  color: #fecaca;
}

body.dark-mode .status-pill.done {
  background: #11371f;
  color: #bbf7d0;
}

.link-button {
  min-height: 34px;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  color: var(--blue-dark);
  background: transparent;
  border: 2px solid var(--border);
  box-shadow: none;
  font-size: 0.88rem;
}

.link-button:hover:not(:disabled),
.link-button:focus-visible:not(:disabled) {
  transform: none;
  color: #ffffff;
  background: var(--blue);
  filter: none;
}

.deck-list-button {
  margin-top: 0.45rem;
}

.action-screen {
  width: min(100% - 2rem, 1760px);
  min-height: calc(100vh - 86px);
  margin: 0 auto;
  padding: 0.3rem 0 0.9rem;
}

.action-card {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 1rem;
  min-height: calc(100vh - 96px);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: clamp(1rem, 1.8vw, 1.5rem);
  background: var(--card);
  box-shadow: var(--shadow);
}

.action-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  border-bottom: 2px solid var(--border);
  padding-bottom: 0.8rem;
}

.action-heading h1 {
  margin: 0;
  font-size: clamp(1.75rem, 2.7vw, 3rem);
  line-height: 1;
}

.screen-kicker,
.action-note {
  margin: 0 0 0.25rem;
  color: var(--muted);
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.secondary-button {
  color: #ffffff;
  background: #64748b;
  box-shadow: 0 5px 0 #475569;
}

.action-visual {
  display: grid;
  place-items: center;
  align-content: center;
  gap: 1rem;
  min-height: 0;
  border: 3px dashed var(--border);
  border-radius: 24px;
  background: var(--panel);
  overflow: hidden;
}

.action-dice-display {
  display: flex;
  justify-content: center;
  gap: clamp(1rem, 2vw, 2rem);
  perspective: 900px;
}

.action-die {
  display: grid;
  place-items: center;
  width: clamp(150px, 13vw, 230px);
  height: clamp(150px, 13vw, 230px);
  border: 8px solid var(--ink);
  border-radius: 28px;
  background: var(--die-bg);
  font-size: clamp(4rem, 8vw, 8rem);
  font-weight: 900;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.15);
}

.action-die.is-hidden {
  display: none;
}

.action-result {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.3rem, 2vw, 2.1rem);
  font-weight: 900;
  text-align: center;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.action-buttons .primary-button,
.action-buttons .secondary-button {
  min-width: min(320px, 100%);
}

.action-coin {
  display: grid;
  place-items: center;
  width: clamp(190px, 18vw, 310px);
  height: clamp(190px, 18vw, 310px);
  border: 14px solid #d99a00;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff7ad 0, var(--yellow) 42%, #d99a00 100%);
  color: #5f3b00;
  font-size: clamp(3.2rem, 6vw, 6rem);
  font-weight: 900;
  box-shadow: inset 0 0 0 8px rgba(255, 255, 255, 0.38), 0 24px 46px rgba(0, 0, 0, 0.2);
  transform-style: preserve-3d;
}

.card-table-card {
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
}

.card-table {
  display: grid;
  grid-template-columns: repeat(8, minmax(92px, 1fr));
  gap: 0.85rem;
  align-content: start;
  min-height: 0;
  padding: 1rem;
  border: 3px solid #2f855a;
  border-radius: 24px;
  background:
    radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.12), transparent 30%),
    linear-gradient(135deg, #14532d, #166534);
  overflow: auto;
}

.table-card {
  position: relative;
  min-height: 132px;
  padding: 0;
  color: var(--ink);
  background: transparent;
  box-shadow: none;
  perspective: 900px;
}

.table-card:hover:not(:disabled),
.table-card:focus-visible:not(:disabled) {
  transform: translateY(-4px);
  filter: none;
}

.table-card:disabled {
  opacity: 1;
}

.table-card-inner {
  position: absolute;
  inset: 0;
  display: block;
  transition: transform 0.75s ease;
  transform-style: preserve-3d;
}

.table-card-back,
.table-card-front {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border: 4px solid #ffffff;
  border-radius: 16px;
  backface-visibility: hidden;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.25);
}

.table-card-back {
  background:
    linear-gradient(45deg, rgba(255, 255, 255, 0.12) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.12) 50%, rgba(255, 255, 255, 0.12) 75%, transparent 75%),
    linear-gradient(135deg, #1d4ed8, #2563eb);
  background-size: 24px 24px, auto;
  color: #ffffff;
  font-size: 3rem;
  font-weight: 900;
}

.table-card-front {
  align-content: center;
  gap: 0.25rem;
  padding: 0.65rem;
  background: var(--routine-bg);
  color: var(--ink);
  text-align: center;
  transform: rotateY(180deg);
}

.table-card-front span {
  color: var(--blue-dark);
  font-weight: 900;
}

.table-card-front strong {
  font-size: clamp(0.9rem, 1vw, 1.08rem);
  line-height: 1.1;
}

.table-card.is-revealed .table-card-inner {
  transform: rotateY(180deg);
}

.table-card.is-dimmed .table-card-inner {
  opacity: 0.38;
  filter: saturate(0.45);
}

.big-rolling .action-die {
  animation: big-dice-roll 0.18s linear infinite;
}

.big-flipping {
  animation: big-coin-flip 0.22s linear infinite;
}

@keyframes big-dice-roll {
  0% { transform: rotate(0deg) translateY(0) scale(1); }
  35% { transform: rotate(10deg) translateY(-16px) scale(1.04); }
  70% { transform: rotate(-8deg) translateY(8px) scale(0.98); }
  100% { transform: rotate(0deg) translateY(0) scale(1); }
}

@keyframes big-coin-flip {
  0% { transform: rotateY(0deg) rotateZ(0deg) scale(1); }
  50% { transform: rotateY(540deg) rotateZ(8deg) scale(1.08); }
  100% { transform: rotateY(1080deg) rotateZ(0deg) scale(1); }
}

@media (max-width: 1150px) {
  .action-screen {
    min-height: auto;
  }

  .action-card {
    min-height: 720px;
  }

  .card-table {
    grid-template-columns: repeat(4, minmax(92px, 1fr));
  }
}

@media (max-width: 700px) {
  .action-heading,
  .action-buttons {
    align-items: stretch;
    flex-direction: column;
  }

  .card-table {
    grid-template-columns: repeat(2, minmax(92px, 1fr));
  }
}

/* ---------- Targeted polish: unclipped panels, clean controls, locked states ---------- */
.result-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.45rem;
  min-height: 286px;
  padding: 0.85rem 0.95rem 0.75rem;
  overflow: visible;
}

.clickable-panel::before {
  content: none;
}

.clickable-panel.is-locked {
  cursor: default;
  border-color: rgba(47, 133, 90, 0.72);
  box-shadow: inset 0 0 0 2px rgba(47, 133, 90, 0.14);
}

.clickable-panel.is-locked:hover,
.clickable-panel.is-locked:focus-visible,
.clickable-panel.is-disabled:hover,
.clickable-panel.is-disabled:focus-visible {
  transform: none;
  box-shadow: inset 0 0 0 2px rgba(47, 133, 90, 0.14);
}

.clickable-panel.is-disabled:not(.is-locked) {
  opacity: 0.78;
}

.panel-action-hint {
  margin: auto 0 0;
  border-radius: 999px;
  padding: 0.22rem 0.65rem;
  background: rgba(35, 100, 170, 0.12);
  color: var(--blue-dark);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  line-height: 1.2;
  text-transform: uppercase;
}

.is-locked .panel-action-hint {
  background: rgba(47, 133, 90, 0.14);
  color: var(--green);
}

.time-display {
  margin-top: 0.15rem;
}

.big-result {
  font-size: clamp(3rem, 5.1vw, 5.7rem);
}

.dice-display {
  margin-top: 0.15rem;
}

.coin {
  flex: 0 0 auto;
  width: clamp(96px, 6.4vw, 120px);
  height: clamp(96px, 6.4vw, 120px);
  margin: 0.35rem auto 0.2rem;
}

.card-panel {
  gap: 0.38rem;
}

.deck-button {
  flex: 0 0 auto;
  margin: 0.2rem auto 0.05rem;
}

.deck {
  width: 74px;
  height: 48px;
  margin: 0.2rem auto;
}

.routine-card {
  width: 100%;
  min-height: 108px;
  padding: 0.55rem 0.65rem;
}

.routine-card strong {
  margin: 0.22rem 0;
  font-size: clamp(1.18rem, 1.85vw, 1.9rem);
  line-height: 1.05;
}

.card-number,
.points-line {
  font-size: 0.98rem;
}

.deck-count-text {
  margin-top: 0.2rem;
  line-height: 1.2;
}

.deck-list-button {
  flex: 0 0 auto;
  min-height: 32px;
  margin-top: 0.05rem;
  padding: 0.28rem 0.7rem;
}

.streamlined-controls {
  display: grid;
  grid-template-columns: minmax(220px, 0.75fr) minmax(320px, 1.25fr);
  gap: 0.75rem 1rem;
  align-items: stretch;
}

.streamlined-controls .primary-button,
.streamlined-controls .add-points-button {
  min-height: 70px;
  font-size: clamp(1.25rem, 1.75vw, 1.8rem);
}

.streamlined-controls .add-points-button {
  width: 100%;
}

.streamlined-controls .manual-note {
  grid-column: 1 / -1;
  min-height: 28px;
  border: 2px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  background: var(--panel);
}


.table-card-inner {
  transition: transform 1.2s ease;
}

.big-rolling .action-die {
  animation: big-dice-roll 0.34s ease-in-out infinite;
}

.big-flipping {
  animation: big-coin-flip 0.42s ease-in-out infinite;
}

@keyframes big-dice-roll {
  0% { transform: rotateX(0deg) rotateZ(0deg) translateY(0) scale(1); }
  25% { transform: rotateX(120deg) rotateZ(12deg) translateY(-20px) scale(1.05); }
  50% { transform: rotateX(240deg) rotateZ(-10deg) translateY(4px) scale(0.99); }
  75% { transform: rotateX(360deg) rotateZ(8deg) translateY(-12px) scale(1.03); }
  100% { transform: rotateX(480deg) rotateZ(0deg) translateY(0) scale(1); }
}

@keyframes big-coin-flip {
  0% { transform: rotateY(0deg) rotateZ(0deg) scale(1); }
  35% { transform: rotateY(720deg) rotateZ(10deg) scale(1.12); }
  70% { transform: rotateY(1440deg) rotateZ(-8deg) scale(1.04); }
  100% { transform: rotateY(2160deg) rotateZ(0deg) scale(1); }
}

body.dark-mode .panel-action-hint {
  background: rgba(96, 165, 250, 0.16);
  color: #bfdbfe;
}

body.dark-mode .is-locked .panel-action-hint {
  background: rgba(125, 211, 167, 0.16);
  color: #bbf7d0;
}

body.demo-mode .streamlined-controls {
  grid-template-columns: minmax(190px, 0.8fr) minmax(300px, 1.2fr);
}

body.demo-mode .streamlined-controls .primary-button,
body.demo-mode .streamlined-controls .add-points-button {
  min-height: 64px;
  font-size: clamp(1.1rem, 1.45vw, 1.55rem);
}

@media (max-width: 900px) {
  .streamlined-controls {
    grid-template-columns: 1fr;
  }

  .streamlined-controls .manual-note {
    grid-column: auto;
  }
}

.table-card.no-animation .table-card-inner {
  transition: none;
}

/* ---------- Decelerating action animations and persistent table spaces ---------- */
.big-rolling.is-decelerating .action-die {
  animation-duration: 0.78s;
  animation-timing-function: ease-out;
}

.big-flipping.is-decelerating {
  animation-duration: 0.9s;
  animation-timing-function: ease-out;
}

.table-card-empty {
  min-height: 132px;
  border: 3px dashed rgba(255, 255, 255, 0.45);
  border-radius: 16px;
  background: rgba(15, 23, 42, 0.18);
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.12);
}

.table-card-empty::after {
  content: "Taken";
  display: grid;
  place-items: center;
  height: 100%;
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.8rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

body.dark-mode .table-card-empty {
  border-color: rgba(203, 213, 225, 0.35);
  background: rgba(2, 6, 23, 0.3);
}

/* Make the action coin visibly coast to a stop instead of switching from fast spin to still. */
.big-flipping,
.big-flipping.is-decelerating {
  animation: big-coin-flip-slowdown 2.2s linear 1 both;
}

@keyframes big-coin-flip-slowdown {
  0% { transform: rotateY(0deg) rotateZ(0deg) scale(1); }
  18% { transform: rotateY(900deg) rotateZ(14deg) scale(1.12); }
  34% { transform: rotateY(1530deg) rotateZ(-12deg) scale(1.09); }
  50% { transform: rotateY(1980deg) rotateZ(9deg) scale(1.06); }
  66% { transform: rotateY(2250deg) rotateZ(-6deg) scale(1.035); }
  80% { transform: rotateY(2410deg) rotateZ(4deg) scale(1.015); }
  91% { transform: rotateY(2480deg) rotateZ(-2deg) scale(1.005); }
  97% { transform: rotateY(2510deg) rotateZ(1deg) scale(1); }
  100% { transform: rotateY(2520deg) rotateZ(0deg) scale(1); }
}
