:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-strong: #eef3f8;
  --text: #172033;
  --muted: #667085;
  --line: #d8e0ea;
  --primary: #155eef;
  --primary-dark: #0f46b8;
  --primary-soft: #e7efff;
  --ok: #087443;
  --ok-soft: #dff7eb;
  --warn: #b54708;
  --warn-soft: #fff2d6;
  --bad: #b42318;
  --bad-soft: #ffe7e5;
  --shadow: 0 12px 30px rgba(23, 32, 51, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button {
  font: inherit;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(16px, 4vw, 36px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  background:
    radial-gradient(circle at 14% 0%, rgba(32, 164, 100, 0.34), transparent 30%),
    linear-gradient(100deg, #111c18 0%, #172033 56%, #263244 100%);
  color: #fff;
  box-shadow: 0 12px 30px rgba(23, 32, 51, 0.16);
  backdrop-filter: blur(12px);
}

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

h1 {
  font-size: clamp(1.25rem, 2vw, 1.7rem);
  font-weight: 800;
}

h2 {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
  font-weight: 800;
}

h3 {
  font-size: 0.95rem;
}

.subtitle {
  margin-top: 2px;
  color: rgba(255, 255, 255, 0.68);
  font-size: 0.83rem;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.07));
  color: #dff7eb;
  font-size: 1.08rem;
  font-weight: 900;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

.header-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  color: #fff;
  font-weight: 700;
}

.header-stats span {
  min-height: 32px;
  padding: 7px 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  white-space: nowrap;
}

.header-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 7px 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  color: #172033;
  font-size: 0.9rem;
  font-weight: 800;
  text-decoration: none;
  white-space: nowrap;
}

.header-link::before {
  content: "←";
  font-weight: 900;
}

.header-link:hover {
  border-color: rgba(255, 255, 255, 0.42);
  background: #fff;
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  width: min(960px, 100%);
  margin: 0 auto;
  padding: 18px clamp(12px, 3vw, 28px) 32px;
}

.problem-panel,
.answer-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.problem-panel,
.answer-panel {
  padding: 12px 14px 14px;
}

.answer-panel {
  align-self: start;
}

.answer-panel .section-head {
  min-height: 38px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.mode-switch {
  display: inline-flex;
  gap: 4px;
  margin-bottom: 10px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
}

.mode-button {
  min-height: 32px;
  padding: 0 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
  cursor: pointer;
}

.mode-button.active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 1px 4px rgba(23, 32, 51, 0.12);
}

.mode-button:focus-visible,
.tile-button:focus-visible,
.button:focus-visible {
  outline: 3px solid rgba(21, 94, 239, 0.25);
  outline-offset: 2px;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.tag {
  padding: 3px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-strong);
  font-size: 0.78rem;
  font-weight: 700;
}

.question-index {
  min-height: 28px;
  padding: 4px 9px;
  border: 1px solid rgba(21, 94, 239, 0.16);
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-size: 0.82rem;
  font-weight: 900;
}

.hand-tiles {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 14px;
  border: 1px solid #b8c4d5;
  border-radius: 8px;
  background: linear-gradient(180deg, #f9fbfd 0%, #eef3f8 100%);
}

.tile {
  display: inline-grid;
  place-items: center;
  position: relative;
}

.tile img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.tile-fallback {
  display: none;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #fff;
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 800;
}

.tile.image-failed img {
  display: none;
}

.tile.image-failed .tile-fallback {
  display: flex;
}

.hand-tile {
  width: 48px;
  height: 67px;
  padding: 2px;
  border: 1px solid #c9d4e2;
  border-radius: 7px;
  background: #fff;
  filter: drop-shadow(0 2px 2px rgba(23, 32, 51, 0.18));
}

.prompt {
  margin-top: 12px;
  color: var(--text);
  font-weight: 700;
}

.selection-status {
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid rgba(21, 94, 239, 0.13);
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary-dark);
  font-size: 0.82rem;
  font-weight: 700;
}

.tile-groups {
  display: grid;
  gap: 0;
}

.tile-group {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding: 11px 0;
  border-top: 1px solid var(--line);
}

.tile-group:first-child {
  border-top: 0;
}

.tile-group-title {
  display: grid;
  place-items: center;
  min-height: 30px;
  border-radius: 8px;
  background: #f5f8fb;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 900;
}

.choice-grid {
  display: grid;
  grid-template-columns: repeat(9, 50px);
  justify-content: start;
  gap: 7px;
}

.honor-grid {
  grid-template-columns: repeat(7, 50px);
}

.tile-button {
  position: relative;
  width: 50px;
  height: 56px;
  min-width: 44px;
  min-height: 44px;
  padding: 3px;
  border: 1px solid #d1dae6;
  border-radius: 8px;
  background: linear-gradient(180deg, #fff 0%, #f7f9fb 100%);
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(23, 32, 51, 0.08);
  transition: border-color 0.12s ease, background 0.12s ease, transform 0.12s ease;
}

.tile-button:hover:not(:disabled) {
  border-color: #97a6ba;
  transform: translateY(-1px);
}

.tile-button.selected {
  border-color: var(--primary);
  background: var(--primary-soft);
  box-shadow: 0 0 0 2px rgba(21, 94, 239, 0.14);
}

.tile-button.answer-correct {
  border-color: #20a464;
  background:
    repeating-linear-gradient(
      135deg,
      rgba(8, 116, 67, 0.13) 0,
      rgba(8, 116, 67, 0.13) 6px,
      rgba(223, 247, 235, 0.92) 6px,
      rgba(223, 247, 235, 0.92) 12px
    );
  box-shadow: 0 0 0 2px rgba(32, 164, 100, 0.22);
}

.tile-button.answer-extra {
  border-color: var(--line);
  background: #fff;
  box-shadow: none;
}

.tile-button.answer-extra.selected::after {
  background: var(--bad);
}

.tile-button.selected::after {
  content: "✓";
  position: absolute;
  top: -7px;
  right: -7px;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 900;
}

.tile-button:disabled {
  cursor: not-allowed;
  opacity: 1;
}

.hand-tiles.discard-hand {
  flex-wrap: nowrap;
  align-items: center;
  overflow-x: auto;
  padding-bottom: 18px;
}

.hand-tiles .tile-button {
  flex: 0 0 auto;
  width: 48px;
  height: 67px;
  padding: 2px;
  border-width: 1px;
}

.tile-button .hand-tile {
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  filter: none;
}

.hand-tiles .tile-button.answer-extra {
  border-color: var(--bad);
  background: var(--bad-soft);
}

.choice-tile {
  width: 31px;
  height: 43px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.button {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  font-weight: 800;
  cursor: pointer;
}

.button.primary {
  background: var(--primary);
  color: #fff;
}

.button.primary:hover {
  background: var(--primary-dark);
}

.button.secondary {
  border-color: var(--line);
  background: #fff;
  color: var(--text);
}

.button.next {
  background: var(--ok);
  color: #fff;
}

.answer-details {
  display: grid;
  gap: 12px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.inline-message {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--warn);
  border-radius: 8px;
  background: var(--warn-soft);
  color: var(--warn);
  font-weight: 800;
}

.detail-section {
  display: grid;
  gap: 8px;
}

.mini-tiles {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 4px;
  vertical-align: middle;
}

.mini-tile {
  width: 24px;
  height: 34px;
}

.evaluation-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.evaluation-table {
  width: 100%;
  min-width: 440px;
  border-collapse: collapse;
  background: #fff;
  font-size: 0.86rem;
}

.evaluation-table th,
.evaluation-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.evaluation-table th {
  color: var(--muted);
  background: var(--surface-strong);
  font-weight: 800;
  white-space: nowrap;
}

.evaluation-table tr:last-child td {
  border-bottom: 0;
}

.evaluation-table .best-row {
  background: var(--ok-soft);
}

.compact-result-table {
  min-width: 360px;
}

.unknown-data {
  color: var(--muted);
  font-size: 0.82rem;
  word-break: break-word;
}

.load-error.in-panel {
  max-width: none;
  margin: 0;
}

.hidden {
  display: none !important;
}

.result-toast {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  pointer-events: none;
  background: rgba(23, 32, 51, 0.14);
}

.result-toast-content {
  min-width: 220px;
  padding: 18px 24px;
  border-radius: 8px;
  color: #fff;
  text-align: center;
  font-size: 1.7rem;
  font-weight: 900;
  box-shadow: 0 18px 46px rgba(23, 32, 51, 0.22);
}

.result-toast.correct .result-toast-content {
  background: var(--ok);
}

.result-toast.incorrect .result-toast-content {
  background: var(--bad);
}

.load-error {
  max-width: 720px;
  margin: 32px auto;
  padding: 20px;
  border: 1px solid var(--bad);
  border-radius: 8px;
  background: var(--bad-soft);
  color: var(--bad);
  font-weight: 800;
}

@media (max-width: 1080px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .header-stats {
    justify-content: flex-start;
  }
}

@media (max-width: 720px) {
  .tile-group {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .tile-group-title {
    justify-self: start;
    padding: 4px 10px;
  }

  .choice-grid,
  .honor-grid {
    grid-template-columns: repeat(auto-fit, 50px);
  }
}

@media (max-width: 560px) {
  .layout {
    padding-inline: 10px;
  }

  .problem-panel,
  .answer-panel {
    padding: 12px;
  }

  .hand-tiles {
    gap: 6px;
    padding: 10px;
  }

  .hand-tile {
    width: 42px;
    height: 59px;
  }

  .choice-grid,
  .honor-grid {
    grid-template-columns: repeat(auto-fit, 47px);
  }

  .tile-button {
    width: 47px;
    height: 53px;
  }

  .hand-tiles .tile-button {
    width: 42px;
    height: 59px;
  }

  .choice-tile {
    width: 29px;
    height: 40px;
  }

  .button {
    flex: 1 1 140px;
  }
}
