:root {
  --bg: #ffffff;
  --fg: #1a1a1b;
  --muted: #6b6f73;
  --border: #d3d6da;
  --border-filled: #878a8c;
  --key-bg: #d3d6da;
  --key-fg: #1a1a1b;
  --panel: #f4f5f6;
  --correct: #4caf50;
  --present: #d4a514;
  --absent: #787c7e;
  --accent: #2f6fde;
  --error: #c62828;
  --toast-bg: #1a1a1b;
  --toast-fg: #ffffff;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121213;
    --fg: #f2f2f2;
    --muted: #a0a3a6;
    --border: #3a3a3c;
    --border-filled: #636366;
    --key-bg: #818384;
    --key-fg: #ffffff;
    --panel: #1e1e20;
    --correct: #538d4e;
    --present: #b59f3b;
    --absent: #3a3a3c;
    --accent: #6a9cff;
    --error: #ff6b6b;
    --toast-bg: #f2f2f2;
    --toast-fg: #121213;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- header ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px;
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  font-size: 1.4rem;
  border-radius: 8px;
}
.icon-btn:hover { background: var(--panel); }

/* ---------- main ---------- */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 8px 16px 12px;
  position: relative;
}

.mode-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 40px;
}

#mode-label { color: var(--muted); font-size: 0.95rem; }

.pill-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
}
.pill-btn:hover { background: var(--panel); }
.pill-btn.attention {
  background: var(--correct);
  border-color: var(--correct);
  color: #fff;
}

.toast {
  position: absolute;
  top: 56px;
  left: 50%;
  transform: translate(-50%, -8px);
  z-index: 5;
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 700;
  opacity: 0;
  pointer-events: none;
  transition: opacity 150ms, transform 150ms;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- board ---------- */

.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 8px 0;
}

.board {
  --cols: 5;
  --rows: 6;
  --gap: 6px;
  --screen-h: 100vh;
  --tile: min(
    62px,
    calc((100vw - 32px - (var(--cols) - 1) * var(--gap)) / var(--cols)),
    calc((var(--screen-h) - 330px - (var(--rows) - 1) * var(--gap)) / var(--rows))
  );
  display: grid;
  gap: var(--gap);
}
@supports (height: 100dvh) {
  .board { --screen-h: 100dvh; }
}

.row {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--tile));
  gap: var(--gap);
}

.tile {
  width: var(--tile);
  height: var(--tile);
  min-width: 24px;
  min-height: 24px;
  display: grid;
  place-items: center;
  border: 2px solid var(--border);
  font-size: calc(var(--tile) * 0.55);
  font-weight: 800;
  text-transform: uppercase;
  user-select: none;
}
.tile.filled { border-color: var(--border-filled); }
.tile.correct, .tile.present, .tile.absent { color: #fff; }
.tile.correct { background: var(--correct); border-color: var(--correct); }
.tile.present { background: var(--present); border-color: var(--present); }
.tile.absent  { background: var(--absent);  border-color: var(--absent); }

.tile.pop    { animation: pop 100ms ease-out; }
.tile.flip   { animation: flip 500ms ease-in-out; }
.tile.bounce { animation: bounce 600ms ease both; }
.row.shake   { animation: shake 400ms; }

@keyframes pop { 50% { transform: scale(1.1); } }
@keyframes flip {
  0%   { transform: rotateX(0); }
  50%  { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}
@keyframes bounce {
  0%, 20%, 60%, 100% { transform: translateY(0); }
  40% { transform: translateY(-24px); }
  80% { transform: translateY(-8px); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}

@media (prefers-reduced-motion: reduce) {
  .tile.pop, .tile.flip, .tile.bounce, .row.shake { animation: none; }
}

/* ---------- keyboard ---------- */

.keyboard {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  user-select: none;
}

.key-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

.key {
  flex: 1;
  max-width: 44px;
  height: 56px;
  height: min(56px, 7vh);
  min-height: 42px;
  border: none;
  border-radius: 6px;
  background: var(--key-bg);
  color: var(--key-fg);
  font-weight: 700;
  font-size: 1.05rem;
  padding: 0;
  touch-action: manipulation;
}
.key.wide { flex: 1.5; max-width: 68px; font-size: 0.8rem; }
.key:active { filter: brightness(0.9); }
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent  { background: var(--absent);  color: #fff; }

/* ---------- dialogs ---------- */

.sheet {
  width: min(520px, calc(100% - 32px));
  max-height: calc(100% - 32px);
  border: none;
  border-radius: 14px;
  padding: 16px 20px 20px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.5); }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.sheet-head h2 { margin: 0; font-size: 1.25rem; }

.guess-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  font-weight: 600;
  font-size: 0.95rem;
}
.guess-setting select {
  font: inherit;
  color: var(--fg);
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  min-width: 72px;
}

.guess-setting small {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-weight: 400;
  font-size: 0.8rem;
}

.switch {
  appearance: none;
  flex: none;
  position: relative;
  width: 46px;
  height: 26px;
  margin: 0;
  border-radius: 999px;
  background: var(--border);
  cursor: pointer;
  transition: background 150ms;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 150ms;
}
.switch:checked { background: var(--correct); }
.switch:checked::after { transform: translateX(20px); }
.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--panel);
  border-radius: 10px;
  margin-bottom: 12px;
}
.tab {
  flex: 1;
  border: none;
  background: none;
  padding: 8px 4px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--muted);
}
.tab[aria-selected="true"] {
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.hint { color: var(--muted); font-size: 0.9rem; margin: 0 0 12px; }

.grade-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.grade-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  text-align: left;
}
.grade-btn:hover { border-color: var(--correct); }
.grade-btn strong { font-size: 1.05rem; }
.grade-btn span { color: var(--muted); font-size: 0.85rem; }

.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 0.9rem; }
.field input,
.field textarea,
.link-out input,
#insert-grade {
  font: inherit;
  font-weight: 400;
  color: var(--fg);
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
}
.field input { font-size: 1.3rem; letter-spacing: 0.15em; text-transform: uppercase; }
.field textarea { resize: vertical; min-height: 120px; }
.field input:focus,
.field textarea:focus { outline: none; border-color: var(--accent); }

.check { display: inline-flex; align-items: center; gap: 6px; margin-top: 8px; font-size: 0.9rem; }

.error { color: var(--error); font-size: 0.9rem; margin: 8px 0 0; }

.list-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
}
.list-tools .hint { margin: 0; flex: 1; min-width: 0; }
#insert-grade { width: auto; padding: 6px 8px; font-size: 0.85rem; }

.actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.primary-btn,
.secondary-btn {
  flex: 1;
  min-width: 140px;
  padding: 12px 16px;
  border-radius: 10px;
  font-weight: 700;
}
.primary-btn { background: var(--correct); color: #fff; border: 2px solid var(--correct); }
.secondary-btn { background: var(--bg); border: 2px solid var(--border); }
.secondary-btn:hover { border-color: var(--border-filled); }

.link-out { margin-top: 12px; }
.link-out input { font-size: 0.85rem; }
.link-status { display: block; margin-top: 6px; color: var(--muted); font-size: 0.85rem; }

.legend { list-style: none; padding: 0; display: grid; gap: 10px; }
.legend li { display: flex; align-items: center; gap: 10px; }
.mini {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 800;
}
.mini.correct { background: var(--correct); }
.mini.present { background: var(--present); }
.mini.absent  { background: var(--absent); }

@media (max-width: 380px) {
  .grade-grid { grid-template-columns: 1fr; }
  .topbar h1 { font-size: 1.25rem; }
}
