:root {
  color-scheme: light;
  --background: #ffffff;
  --surface: #ffffff;
  --surface-muted: #f6f7f8;
  --text: #121213;
  --muted: #787c7e;
  --border: #d3d6da;
  --border-strong: #878a8c;
  --green: #6aaa64;
  --green-dark: #538d4e;
  --yellow: #c9b458;
  --red: #c95757;
  --blue: #4d7caa;
  --key: #d3d6da;
  --overlay: rgba(255, 255, 255, 0.94);
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.08);
  --tile-size: clamp(42px, 10.5vw, 62px);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --background: #121213;
  --surface: #121213;
  --surface-muted: #202124;
  --text: #f8f8f8;
  --muted: #a4a6a8;
  --border: #3a3a3c;
  --border-strong: #818384;
  --green: #538d4e;
  --green-dark: #6aaa64;
  --yellow: #b59f3b;
  --red: #a94444;
  --blue: #5c88b2;
  --key: #818384;
  --overlay: rgba(18, 18, 19, 0.94);
  --shadow: 0 12px 34px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--background);
  font-family: Arial, Helvetica, sans-serif;
  transition: color 180ms ease, background-color 180ms ease;
}

button, input { font: inherit; }
button { color: inherit; }

.app-shell {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
}

.topbar {
  position: sticky;
  z-index: 10;
  top: 0;
  border-bottom: 1px solid var(--border);
  background: var(--overlay);
  backdrop-filter: blur(14px);
}

.topbar-inner {
  display: grid;
  width: min(100%, 560px);
  min-height: 58px;
  margin: 0 auto;
  padding: 0 12px;
  grid-template-columns: 42px 1fr 42px;
  align-items: center;
}

.topbar-spacer { width: 42px; }

.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--text);
  text-decoration: none;
}

.brand-tile {
  display: grid;
  width: 27px;
  height: 27px;
  place-items: center;
  color: white;
  background: var(--green);
  font-size: 0.9rem;
  font-weight: 800;
  line-height: 1;
}

.brand-tile.accent { background: var(--yellow); }

.brand-name {
  margin-left: 6px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.55rem;
  font-weight: 800;
  letter-spacing: -0.04em;
}

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  padding: 9px;
  place-items: center;
  border: 0;
  border-radius: 50%;
  color: var(--text);
  background: transparent;
  cursor: pointer;
}

.icon-button:hover { background: var(--surface-muted); }
.icon-button svg, .secondary-button svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.moon-icon { display: block; }
.sun-icon { display: none; }
html[data-theme="dark"] .moon-icon { display: none; }
html[data-theme="dark"] .sun-icon { display: block; }

.game {
  width: min(100% - 28px, 520px);
  margin: 0 auto;
  padding: 34px 0 18px;
}

.intro { text-align: center; }
.kicker {
  margin: 0 0 7px;
  color: var(--green-dark);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2.15rem, 9vw, 3.15rem);
  line-height: 1;
  letter-spacing: -0.045em;
}

.intro > p:last-child {
  max-width: 440px;
  margin: 14px auto 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.guess-form { margin-top: 34px; }

.word-section {
  position: relative;
  padding: 18px 18px 20px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.word-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.word-heading > div {
  display: flex;
  align-items: center;
  gap: 9px;
}

.word-heading h2 {
  margin: 0;
  font-size: 0.95rem;
  letter-spacing: 0.01em;
}

.language-code {
  display: inline-grid;
  min-width: 29px;
  height: 24px;
  padding: 0 5px;
  place-items: center;
  border-radius: 3px;
  color: white;
  background: var(--blue);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.08em;
}
.language-code.italian { background: var(--green); }

.length-badge {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

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

.tile-board {
  display: grid;
  grid-template-columns: repeat(var(--tile-count), minmax(0, var(--tile-size)));
  justify-content: center;
  gap: clamp(3px, 1vw, 6px);
  cursor: text;
  outline: none;
}

.letter-tile {
  display: grid;
  width: min(var(--tile-size), calc(440px / var(--tile-count)), calc((100vw - 58px) / var(--tile-count)));
  max-width: var(--tile-size);
  aspect-ratio: 1;
  place-items: center;
  border: 2px solid var(--border);
  color: var(--text);
  background: var(--surface);
  font-size: clamp(1.35rem, 6vw, 2rem);
  font-weight: 800;
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
  transition: border-color 120ms ease, transform 120ms ease, background-color 180ms ease, color 180ms ease;
}

.letter-tile.fixed {
  border-color: var(--border-strong);
  color: white;
  background: var(--border-strong);
}

.letter-tile.filled:not(.fixed) {
  border-color: var(--border-strong);
  animation: pop 90ms ease-out;
}

.tile-board.active .letter-tile:not(.fixed) { border-color: var(--border-strong); }
.tile-board.active .letter-tile:empty:first-of-type ~ .letter-tile:empty { border-color: var(--border); }
.tile-board.complete .letter-tile { border-color: var(--border-strong); }

.tile-board.success .letter-tile,
.tile-board.revealed .letter-tile {
  border-color: var(--green);
  color: white;
  background: var(--green);
  animation: flip 500ms ease both;
}

.tile-board.success .letter-tile:nth-child(2),
.tile-board.revealed .letter-tile:nth-child(2) { animation-delay: 70ms; }
.tile-board.success .letter-tile:nth-child(3),
.tile-board.revealed .letter-tile:nth-child(3) { animation-delay: 140ms; }
.tile-board.success .letter-tile:nth-child(4),
.tile-board.revealed .letter-tile:nth-child(4) { animation-delay: 210ms; }
.tile-board.success .letter-tile:nth-child(5),
.tile-board.revealed .letter-tile:nth-child(5) { animation-delay: 280ms; }
.tile-board.success .letter-tile:nth-child(n+6),
.tile-board.revealed .letter-tile:nth-child(n+6) { animation-delay: 350ms; }

.tile-board.error .letter-tile {
  border-color: var(--red);
  color: white;
  background: var(--red);
}

.tile-input {
  position: absolute;
  left: 50%;
  bottom: 10px;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

.pair-divider {
  display: grid;
  height: 36px;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  color: var(--muted);
}
.pair-divider span { height: 1px; background: var(--border); }
.pair-divider b { font-size: 1rem; }

.active-hint {
  min-height: 20px;
  margin: 14px 0 11px;
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
}

.primary-button {
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: 5px;
  color: white;
  background: var(--green);
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
  transition: filter 120ms ease, transform 120ms ease;
}
.primary-button:hover { filter: brightness(0.95); }
.primary-button:active { transform: translateY(1px); }
.primary-button:disabled, .secondary-button:disabled { cursor: wait; opacity: 0.65; }

.toast {
  min-height: 44px;
  margin: 14px auto 0;
  padding: 12px 14px;
  border-radius: 5px;
  color: var(--text);
  background: var(--surface-muted);
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
}
.toast:empty { visibility: hidden; }
.toast.success { color: white; background: var(--green); }
.toast.error { color: white; background: var(--red); }

.game-actions {
  display: grid;
  margin-top: 10px;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.secondary-button {
  display: inline-flex;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 0;
  border-radius: 5px;
  color: var(--text);
  background: var(--key);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
}
html:not([data-theme="dark"]) .secondary-button { color: #121213; }
.secondary-button svg { width: 17px; height: 17px; }

.rules {
  margin-top: 24px;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.rules summary {
  padding: 15px 2px;
  font-size: 0.84rem;
  font-weight: 800;
  cursor: pointer;
}
.rules-content {
  padding: 0 2px 16px;
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.5;
}
.rules-content p { margin: 0 0 12px; }
.rule-example { display: flex; gap: 4px; margin-bottom: 12px; }
.mini-tile {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border: 2px solid var(--border-strong);
  color: var(--text);
  font-weight: 800;
}
.mini-tile.fixed { color: white; background: var(--border-strong); }
.mini-tile.filled { color: white; border-color: var(--green); background: var(--green); }

.footer {
  display: flex;
  margin-top: auto;
  padding: 20px 16px 24px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  color: var(--muted);
  font-size: 0.72rem;
}

@keyframes pop {
  0% { transform: scale(0.88); }
  100% { transform: scale(1); }
}

@keyframes flip {
  0% { transform: rotateX(0); }
  45% { transform: rotateX(90deg); }
  55% { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}

.tile-board.shake { animation: shake 420ms ease; }

@media (max-width: 430px) {
  :root { --tile-size: clamp(36px, 10.8vw, 46px); }
  .game { width: min(100% - 20px, 520px); padding-top: 24px; }
  .word-section { padding: 15px 10px 17px; }
  .brand-name { font-size: 1.35rem; }
  .brand-tile { width: 24px; height: 24px; font-size: 0.8rem; }
  .intro > p:last-child { font-size: 0.84rem; }
}

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