:root {
  --blue: #149bd7;
  --ink: #102131;
  --muted: #5c6b78;
  --paper: #eef4f8;
  --panel: #ffffff;
  --line: #d8e2ea;
  --dark: #071018;
  --green: #1d8f5a;
  --amber: #f3b23a;
  --red: #d84f4f;
  --violet: #6d5fd0;
  --shadow: 0 12px 32px rgba(15, 33, 49, 0.14);
  --radius: 8px;
  --touch: 48px;
  font-family: Arial, Helvetica, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html,
body {
  height: 100%;
  min-height: 0;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    linear-gradient(#dce8f0 1px, transparent 1px),
    linear-gradient(90deg, #dce8f0 1px, transparent 1px),
    var(--paper);
  background-size: 34px 34px;
  touch-action: manipulation;
}

button,
select,
input { font: inherit; }

button,
select,
summary,
.rcs-settings label { -webkit-tap-highlight-color: transparent; }

button {
  min-height: var(--touch);
  border: 2px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
}

button:focus-visible,
select:focus-visible,
summary:focus-visible,
input:focus-visible {
  outline: 4px solid rgba(20, 155, 215, 0.35);
  outline-offset: 2px;
}

button:disabled { cursor: default; }

select {
  min-height: var(--touch);
  padding: 0 12px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  color: var(--ink);
  font-weight: 800;
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  min-height: 0;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 7px 14px;
  background: var(--dark);
  color: #ffffff;
}

.brand,
.top-actions,
.toolbar,
.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.brand { min-width: 0; gap: 12px; }
.brand-logo { display: inline-flex; border-radius: 6px; }
.top-actions { justify-content: end; }

h1 {
  margin: 0;
  font-size: clamp(1.3rem, 2vw, 2rem);
  line-height: 1.05;
}

.icon-button {
  width: var(--touch);
  padding: 0;
  display: inline-grid;
  place-items: center;
  border-color: #263d51;
  background: #122436;
  color: #ffffff;
}

.icon-button:hover,
.home-button:hover,
.home-button:focus-visible {
  border-color: #8fd8f3;
  background: var(--blue);
  color: #06111a;
}

.icon { width: 24px; height: 24px; pointer-events: none; }

.home-button {
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  border: 2px solid #263d51;
  border-radius: var(--radius);
  background: #122436;
  color: #ffffff;
  text-decoration: none;
  font-weight: 800;
}

#roundingCardSortGame {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
}

.toolbar {
  position: relative;
  justify-content: center;
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
  box-shadow: 0 6px 14px rgba(15, 33, 49, 0.06);
  z-index: 5;
}

.wide-button,
.tool-menu > summary {
  min-height: var(--touch);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 13px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #f8fbfd;
  color: var(--ink);
  font-weight: 900;
  white-space: nowrap;
}

.wide-button:hover,
.tool-menu > summary:hover { border-color: #8fbfd8; background: #f3faff; }

.wide-button.primary {
  border-color: #4d409f;
  background: var(--violet);
  color: #ffffff;
}

.rcs-print-button { border-color: #6d5fd0; color: #4d409f; }
.rcs-print-button:hover { border-color: #4d409f; background: #f2f0ff; }

@media (min-width: 761px) and (max-width: 1100px) {
  .rcs-toolbar { flex-wrap: nowrap; gap: 4px; padding-inline: 6px; }
  .rcs-toolbar select,
  .rcs-toolbar .wide-button,
  .rcs-toolbar .tool-menu > summary { min-width: 0; padding-inline: 7px; font-size: clamp(0.78rem, 1.55vw, 0.96rem); }
}

.wide-button:disabled { opacity: 0.48; }

.tool-menu { position: relative; }
.tool-menu > summary { list-style: none; cursor: pointer; }
.tool-menu > summary::-webkit-details-marker { display: none; }

.tool-menu .menu-panel {
  position: absolute;
  top: calc(100% + 7px);
  left: 50%;
  width: min(640px, calc(100vw - 24px));
  max-height: min(500px, calc(100vh - 150px));
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr));
  gap: 9px;
  padding: 10px;
  border: 1px solid #b9cbd8;
  border-radius: var(--radius);
  background: #ffffff;
  box-shadow: var(--shadow);
  transform: translateX(-50%);
}

.rcs-settings fieldset {
  min-width: 0;
  margin: 0;
  padding: 8px;
  display: grid;
  gap: 5px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.rcs-settings legend { padding: 0 4px; color: var(--muted); font-size: 0.78rem; font-weight: 900; }
.rcs-settings label { min-height: 37px; display: flex; align-items: center; gap: 7px; padding: 4px 6px; border-radius: 6px; font-size: 0.85rem; font-weight: 800; }
.rcs-settings label:hover { background: #eef8fd; }
.rcs-settings input { width: 20px; height: 20px; margin: 0; accent-color: var(--violet); }

.rcs-shell {
  width: min(1220px, calc(100% - 20px));
  height: 100%;
  min-height: 0;
  margin: 0 auto;
  padding: 8px 0;
  overflow: hidden;
}

.rcs-layout {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}

.rcs-board {
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.rcs-board {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: 10px;
}

.rcs-status-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #fbfdff;
}

.rcs-stat {
  min-height: 52px;
  display: grid;
  place-content: center;
  gap: 2px;
  padding: 5px 10px;
  border-right: 1px solid var(--line);
  text-align: center;
}

.rcs-stat:last-child { border-right: 0; }
.rcs-stat span { color: var(--muted); font-size: 0.7rem; font-weight: 900; text-transform: uppercase; }
.rcs-stat strong { font-size: clamp(1rem, 1.7vw, 1.35rem); }

.rcs-play {
  min-height: 0;
  border: 2px solid #c8d9e5;
  border-radius: var(--radius);
  background: #ffffff;
  overflow: hidden;
}

.rcs-feedback {
  min-height: 48px;
  display: grid;
  place-items: center;
  padding: 7px 11px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: #f8fbfd;
  font-size: 0.93rem;
  font-weight: 850;
  line-height: 1.24;
  text-align: center;
}

.rcs-feedback[data-tone="success"] { border-color: rgba(29, 143, 90, 0.45); background: #edfaf4; color: #145f3d; }
.rcs-feedback[data-tone="error"] { border-color: rgba(216, 79, 79, 0.45); background: #fff0f0; color: #9d2828; }

.rcs-empty { margin: 0; color: var(--muted); font-size: 0.82rem; font-weight: 750; }

.rcs-round-phase {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(210px, 0.72fr) minmax(310px, 1.28fr);
  grid-template-rows: minmax(0, 1fr) auto auto;
  gap: 9px;
  padding: 10px;
}

.rcs-card {
  min-height: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  padding: 18px;
  border: 3px solid #b97bb3;
  border-radius: var(--radius);
  background: linear-gradient(145deg, #fff 50%, #fbf3fa);
  text-align: center;
}

.rcs-card h2 { margin: 0; font-size: clamp(2.4rem, 6vw, 5.4rem); line-height: 0.98; }
.rcs-context { margin: 0; color: #294156; font-size: clamp(0.95rem, 1.5vw, 1.2rem); font-weight: 850; line-height: 1.28; }

.rcs-answer-list { min-height: 0; display: grid; align-content: center; gap: 8px; }
.rcs-answer-row { min-width: 0; margin: 0; padding: 7px; border: 1px solid var(--line); border-radius: var(--radius); }
.rcs-answer-row legend { padding: 0 5px; color: var(--muted); font-size: 0.83rem; font-weight: 900; }
.rcs-answer-choices { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.rcs-answer { min-width: 0; padding: 5px 8px; border-color: #b9cbd8; background: #ffffff; color: var(--ink); font-size: clamp(1rem, 2vw, 1.5rem); font-weight: 900; }
.rcs-answer:hover:not(:disabled) { border-color: var(--blue); background: #eef8fd; }
.rcs-answer.is-selected { border-color: #d49d24; background: #fff7df; }
.rcs-answer.is-correct { border-color: var(--green); background: #edfaf4; color: #145f3d; opacity: 1; }
.rcs-answer.is-incorrect { border-color: var(--red); background: #fff0f0; color: #9d2828; }

.rcs-round-actions { grid-column: 1 / -1; display: flex; justify-content: center; gap: 8px; }
.rcs-hint { grid-column: 1 / -1; min-height: 0; overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: #fbfdff; }
.rcs-hint-model { height: clamp(112px, 21vh, 172px); padding: 2px 4px; }
.rcs-hint-model figure { width: 100%; height: 100%; margin: 0; }
.rcs-hint-model figure figcaption { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.rcs-hint-model svg { width: 100%; height: 100%; display: block; }
.rcs-hint-error { margin: 0; padding: 12px; color: #9d2828; font-weight: 800; text-align: center; }

.rcs-sort-phase { height: 100%; min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 8px; padding: 9px; }
.rcs-sort-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 0 4px; }
.rcs-sort-heading p,
.rcs-sort-heading h2 { margin: 0; }
.rcs-sort-heading p { color: var(--muted); font-size: 0.82rem; font-weight: 900; text-transform: uppercase; }
.rcs-sort-heading h2 { font-size: clamp(1rem, 1.8vw, 1.3rem); text-align: right; }
.rcs-sort-workspace { min-height: 0; display: grid; grid-template-rows: repeat(2, minmax(0, 1fr)); gap: 8px; }
.rcs-sort-workspace.has-multi-results { grid-template-rows: minmax(0, 1.6fr) minmax(0, 1fr); }
.rcs-sort-tray { min-height: 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); grid-auto-rows: minmax(var(--touch), 1fr); align-content: stretch; gap: 5px; padding: 6px; border: 1px solid var(--line); border-radius: var(--radius); background: #f8fbfd; overflow: hidden; }
.rcs-sort-card { min-width: 0; min-height: var(--touch); display: grid; align-content: center; gap: 2px; padding: 4px; border-color: #b9cbd8; background: #ffffff; color: var(--ink); }
.rcs-sort-card strong { font-size: clamp(1.02rem, 1.8vw, 1.35rem); }
.rcs-sort-card span { display: block; color: var(--muted); font-size: 0.65rem; line-height: 1.16; }
.has-multi-results .rcs-sort-card { gap: 0; padding: 3px; }
.has-multi-results .rcs-sort-card > strong { font-size: clamp(0.9rem, 1.5vw, 1.12rem); line-height: 1; }
.has-multi-results .rcs-sort-card span { font-size: clamp(0.48rem, 0.8vw, 0.6rem); line-height: 1; }
.has-multi-results .rcs-sort-card span strong { font-size: inherit; line-height: inherit; }
.rcs-sort-card.is-selected { border-color: var(--amber); background: #fff7df; box-shadow: 0 0 0 2px rgba(243, 178, 58, 0.2); }
.rcs-groups { min-height: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 8px; }
.rcs-group { min-width: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 5px; padding: 7px; border: 1px solid var(--line); border-radius: var(--radius); background: #fbfdff; }
.rcs-group-target { min-height: 54px; padding: 6px 8px; border-color: #aebfcb; background: #eef8fd; color: var(--ink); font-size: 0.82rem; font-weight: 900; line-height: 1.2; }
.rcs-group-target:not(:disabled):hover { border-color: var(--blue); background: #dff3fb; }
.rcs-group-cards { min-height: 0; display: flex; align-content: flex-start; flex-wrap: wrap; gap: 4px; padding: 4px; overflow: hidden; }
.rcs-group-cards > span:not(.rcs-empty) { padding: 3px 6px; border: 1px solid #b9d3e1; border-radius: 6px; background: #ffffff; font-size: clamp(0.66rem, 1.2vw, 0.78rem); font-weight: 850; }

.rcs-complete { height: 100%; display: grid; place-content: center; gap: 10px; padding: 18px; text-align: center; }
.rcs-complete-kicker { margin: 0; color: var(--green); font-size: 0.8rem; font-weight: 900; text-transform: uppercase; }
.rcs-complete h2 { margin: 0; font-size: clamp(2.6rem, 7vw, 5rem); }
.rcs-complete > p:not(.rcs-complete-kicker) { margin: 0; font-size: clamp(1rem, 1.8vw, 1.3rem); font-weight: 850; }
.rcs-complete-groups { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 7px; }
.rcs-complete-groups section { padding: 8px; border: 1px solid var(--line); border-radius: var(--radius); background: #f8fbfd; }
.rcs-complete-groups h3,
.rcs-complete-groups p { margin: 0; }
.rcs-complete-groups h3 { font-size: 0.8rem; }
.rcs-complete-groups p { margin-top: 4px; color: var(--muted); font-size: 0.78rem; }
.rcs-complete .wide-button { justify-self: center; }

@media (max-width: 900px) {
  .rcs-round-phase { grid-template-columns: minmax(180px, 0.62fr) minmax(280px, 1.38fr); }
}

@media (max-height: 700px) and (min-width: 761px) {
  .rcs-sort-tray { gap: 3px; padding: 3px; }
  .rcs-sort-card { padding: 2px; }
}

@media (max-width: 760px), (max-height: 649px) {
  html,
  body { height: auto; min-height: 100%; overflow: auto; }
  .app { height: auto; min-height: 100vh; }
  #roundingCardSortGame { display: block; }
  .topbar { grid-template-columns: 1fr auto; }
  .toolbar { justify-content: flex-start; }
  .tool-menu .menu-panel { position: fixed; top: 76px; left: 12px; right: 12px; width: auto; grid-template-columns: repeat(2, minmax(0, 1fr)); transform: none; }
  .rcs-shell { height: auto; min-height: 650px; overflow: visible; }
  .rcs-layout { height: auto; grid-template-columns: minmax(0, 1fr); }
  .rcs-board { min-height: 610px; }
  .rcs-round-phase { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto auto auto; }
  .rcs-card { min-height: 190px; }
  .rcs-round-actions,
  .rcs-hint { grid-column: 1; }
}

@media (max-width: 520px) {
  .topbar { grid-template-columns: 1fr; }
  .top-actions { justify-content: flex-start; }
  .toolbar .control-group { width: 100%; }
  .toolbar select { flex: 1; min-width: 0; }
  .tool-menu .menu-panel { grid-template-columns: minmax(0, 1fr); }
  .rcs-status-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .rcs-sort-heading { align-items: flex-start; flex-direction: column; }
  .rcs-sort-heading h2 { text-align: left; }
  .rcs-sort-tray { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rcs-groups,
  .rcs-complete-groups { grid-template-columns: minmax(0, 1fr); }
  .rcs-group-cards { min-height: 52px; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after { scroll-behavior: auto !important; transition-duration: 0.001ms !important; animation-duration: 0.001ms !important; }
}
