*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-family);
  background: var(--color-bg);
  color: var(--color-text);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* Header */
.header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 8px;
  height: 50px;
  border-bottom: 1px solid var(--color-header-border);
  flex-shrink: 0;
  position: relative;
  z-index: 102;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 2px;
  justify-self: start;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 2px;
  justify-self: end;
}

.header-title {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.12rem;
  text-transform: uppercase;
  text-align: center;
  white-space: nowrap;
}

.icon-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  padding: 8px;
  border-radius: 4px;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  -webkit-appearance: none;
  appearance: none;
  line-height: 0;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--color-border);
}

.icon-btn:active {
  background: var(--color-border);
}

.icon-btn:focus {
  outline: none;
}

.icon-btn:focus-visible {
  outline: 2px solid var(--color-text-muted);
  outline-offset: 2px;
}

.icon-btn svg {
  display: block;
  width: 24px;
  height: 24px;
  color: inherit;
}

.icon-btn-menu {
  color: var(--color-text);
  background: transparent;
}

.icon-btn-menu .icon-hamburger,
.icon-btn-menu .icon-close {
  transition: opacity 180ms ease, transform 180ms ease;
}

.icon-btn-menu .icon-close {
  display: none;
  opacity: 0;
  transform: rotate(-90deg) scale(0.85);
}

.icon-btn-menu[aria-expanded="true"] .icon-hamburger {
  display: none;
}

.icon-btn-menu[aria-expanded="true"] .icon-close {
  display: block;
  opacity: 1;
  transform: rotate(0deg) scale(1);
}

.icon-hamburger {
  fill: currentColor;
}

.icon-hamburger rect {
  fill: currentColor;
}

/* Game container */
.game-container {
  flex: 1;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  justify-items: center;
  align-content: stretch;
  padding: 8px 4px 12px;
  max-height: calc(100dvh - 50px);
  min-height: 0;
  overflow: hidden;
  width: 100%;
}

.page-container {
  flex: 1;
  padding: 24px;
  max-width: 720px;
  margin: 0 auto;
  overflow-y: auto;
}

/* Board */
.board {
  display: flex;
  flex-direction: column;
  gap: var(--tile-gap);
  align-self: center;
  justify-self: center;
  min-height: 0;
  max-height: 100%;
  overflow: hidden;
}

.board-row {
  display: flex;
  gap: var(--tile-gap);
  justify-content: center;
}

/* Keyboard */
.keyboard {
  display: flex;
  flex-direction: column;
  gap: var(--key-gap);
  width: min(100%, 500px);
  flex-shrink: 0;
  padding: 0 2px;
  box-sizing: border-box;
}

.keyboard-row {
  display: flex;
  gap: var(--key-gap);
  justify-content: center;
  width: 100%;
}

/* Results button */
.btn-results {
  background: var(--color-correct);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 14px 48px;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.08rem;
  cursor: pointer;
  touch-action: manipulation;
  margin: 8px 0;
  flex-shrink: 0;
}

.btn-results:hover {
  opacity: 0.9;
}

/* Toast */
.toast {
  position: fixed;
  top: 58px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: var(--color-toast-bg);
  color: var(--color-toast-text);
  padding: 12px 20px;
  border-radius: 4px;
  font-weight: 600;
  font-size: 0.875rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Modals */
dialog.modal::backdrop {
  background: rgba(0, 0, 0, 0.72);
}

dialog.modal:not(.modal-menu) {
  border: none;
  border-radius: 12px;
  padding: 0;
  width: min(500px, calc(100vw - 24px));
  max-width: min(500px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow: auto;
  background: var(--color-modal-bg);
  color: var(--color-text);
  margin: auto;
  position: fixed;
  inset: 0;
  height: fit-content;
}

dialog.modal-menu {
  margin: 0 0 0 auto;
  max-width: 280px;
  width: 280px;
  height: calc(100dvh - 50px);
  max-height: calc(100dvh - 50px);
  border: none;
  border-radius: 0;
  padding: 0;
  background: var(--color-modal-bg);
  color: var(--color-text);
  box-shadow: -4px 0 24px rgba(0, 0, 0, 0.35);
  position: fixed;
  top: 50px;
  right: 0;
  bottom: 0;
  left: auto;
  z-index: 101;
  transform: translateX(100%);
}

dialog.modal-menu.is-open {
  animation: menu-slide-in 280ms cubic-bezier(0.32, 0.72, 0, 1) forwards;
}

dialog.modal-menu.is-closing {
  animation: menu-slide-out 240ms cubic-bezier(0.32, 0.72, 0, 1) forwards;
}

.menu-backdrop {
  position: fixed;
  top: 50px;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.72);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
}

.menu-backdrop.is-open {
  animation: menu-backdrop-in 280ms ease forwards;
  pointer-events: auto;
}

.menu-backdrop.is-closing {
  animation: menu-backdrop-out 240ms ease forwards;
  pointer-events: none;
}

.modal-content {
  padding: 24px;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--color-text-muted);
  touch-action: manipulation;
  line-height: 1;
}

.modal-content h2 {
  margin-bottom: 16px;
  font-size: 1.25rem;
  font-weight: 700;
}

.modal-help .modal-content h2 {
  text-align: left;
  text-transform: none;
  letter-spacing: normal;
}

.modal-stats .modal-content h2 {
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.08rem;
  font-size: 1.1rem;
}

/* Help modal footer */
.help-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 16px;
  margin-top: 16px;
  border-top: 1px solid var(--color-border);
  font-size: 0.9rem;
  color: var(--color-text-muted);
}

.help-countdown-time {
  font-weight: 700;
  color: var(--color-text);
}

/* Menu panel */
.menu-panel {
  padding: 16px 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.menu-nav {
  display: flex;
  flex-direction: column;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  color: var(--color-menu-link);
  text-decoration: none;
  font-size: 1rem;
  font-weight: 500;
  border: none;
  background: none;
  cursor: pointer;
  width: 100%;
  text-align: left;
  font-family: inherit;
}

.menu-item:hover {
  background: var(--color-border);
}

.menu-item svg {
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.theme-icon-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.theme-icon {
  position: absolute;
  top: 0;
  left: 0;
  width: 20px;
  height: 20px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

html[data-theme="dark"] .theme-icon-moon,
html[data-theme="light"] .theme-icon-sun {
  opacity: 1;
  visibility: visible;
}

html[data-theme="dark"] .theme-icon-moon {
  color: var(--color-text-muted);
}

html[data-theme="light"] .theme-icon-sun {
  color: #fbbf24;
}

.menu-theme-label {
  flex: 1;
  font-size: 1rem;
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
  user-select: none;
}

.menu-divider {
  height: 1px;
  background: var(--color-menu-divider);
  margin: 8px 0;
}

.menu-settings {
  margin-top: auto;
}

.menu-item-static,
.menu-item-toggle,
.menu-item-select {
  color: var(--color-text);
}

.menu-item-static {
  cursor: default;
}

.menu-item-select {
  cursor: default;
  gap: 10px;
}

.menu-item-static:hover,
.menu-item-select:hover {
  background: transparent;
}

.menu-item-toggle:hover {
  background: var(--color-border);
}

.menu-item-value {
  margin-left: auto;
  color: var(--color-text-muted);
  font-size: 0.85rem;
}

.menu-chevron {
  color: var(--color-text-muted);
  margin-left: 4px;
}

.menu-item-toggle {
  cursor: pointer;
  user-select: none;
}

.menu-item-toggle .toggle-switch {
  margin-left: auto;
  flex-shrink: 0;
  pointer-events: auto;
}

.menu-item-label {
  flex-shrink: 0;
  font-size: 1rem;
  font-weight: 500;
  color: var(--color-text);
}

.menu-select {
  margin-left: auto;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 130px;
  height: 32px;
  padding: 0 28px 0 10px;
  background-color: var(--color-border);
  color: var(--color-text);
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 0.875rem;
  font-family: inherit;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='%23818384'%3E%3Cpath d='M7 10l5 5 5-5H7z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 16px;
  color-scheme: light dark;
  transition: border-color 0.15s, background-color 0.15s;
}

.menu-select:hover {
  background-color: var(--color-absent);
  border-color: var(--color-text-muted);
}

.menu-select:focus {
  outline: none;
  border-color: var(--color-text-muted);
  box-shadow: 0 0 0 2px rgba(129, 131, 132, 0.35);
}

.menu-select option {
  background: var(--color-modal-bg);
  color: var(--color-text);
  padding: 8px;
}

html[data-theme="light"] .menu-select {
  color-scheme: light;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='%23787c7e'%3E%3Cpath d='M7 10l5 5 5-5H7z'/%3E%3C/svg%3E");
}

html[data-theme="dark"] .menu-select {
  color-scheme: dark;
}

/* Toggle switch */
.toggle-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--color-border);
  border-radius: 24px;
  transition: 0.2s;
}

.toggle-slider::before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.2s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--color-correct);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(20px);
}

/* Stats modal */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  margin-bottom: 16px;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
}

.stat-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--color-text-muted);
}

.distribution {
  margin-bottom: 16px;
}

.dist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 0.85rem;
}

.dist-bar {
  height: 20px;
  background: var(--color-correct);
  border-radius: 2px;
  min-width: 8px;
  transition: width 0.3s;
  color: #fff;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 4px;
}

.countdown {
  text-align: center;
  font-size: 1.5rem;
}

.stats-countdown-extra {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--color-border);
  text-align: center;
  margin-bottom: 16px;
}

.stats-countdown-label {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  margin-bottom: 6px;
}

.stats-countdown {
  font-size: 1.5rem;
  color: var(--color-text);
  font-weight: 700;
}

.btn {
  display: block;
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

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

.btn-primary:hover {
  opacity: 0.9;
}

.btn-secondary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px 16px;
  border: none;
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  background: var(--color-border);
  color: var(--color-text);
  text-decoration: none;
  font-family: inherit;
}

.btn-secondary:hover {
  opacity: 0.9;
}

.btn-try-again {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px 16px;
  border: none;
  border-radius: 999px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  background: var(--color-correct);
  color: #fff;
  font-family: inherit;
}

.btn-try-again:hover {
  opacity: 0.92;
}

.btn-try-again .btn-icon,
.btn-create-link .btn-icon {
  font-size: 1.1rem;
  line-height: 1;
}

/* Result modal */
.modal-result .modal-content {
  padding: 28px 24px 24px;
  text-align: center;
}

.result-body {
  margin-bottom: 20px;
}

.result-label {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  margin-bottom: 8px;
}

.result-word {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.25rem;
  text-transform: uppercase;
  margin-bottom: 8px;
  word-break: break-word;
}

.result-win-msg {
  font-size: 1rem;
  color: var(--color-correct);
  font-weight: 600;
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.result-daily-extra {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--color-border);
}

.result-countdown-label {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  margin-bottom: 6px;
}

.result-countdown {
  font-size: 1.5rem;
  color: var(--color-text);
  font-weight: 700;
}

.btn-result-unlimited {
  text-decoration: none;
  text-align: center;
}

@media (max-width: 480px) {
  dialog.modal:not(.modal-menu) {
    width: calc(100vw - 16px);
    max-width: calc(100vw - 16px);
    border-radius: 10px;
  }

  .modal-content {
    padding: 20px 16px;
  }

  .result-word {
    font-size: 1.75rem;
  }

  .stats-grid {
    gap: 4px;
  }

  .stat-value {
    font-size: 1.5rem;
  }
}

/* Create panel */
.game-container:has(.create-panel) {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  justify-items: center;
  align-content: stretch;
}

.create-panel {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 16px;
  width: min(100%, 500px);
  margin: 0;
  align-self: center;
}

.game-container:has(.create-panel) .keyboard {
  width: min(100%, 500px);
}

.create-intro {
  color: var(--color-text-muted);
  margin-bottom: 16px;
  font-size: 0.95rem;
}

.create-form {
  display: flex;
  gap: 8px;
  justify-content: center;
  align-items: center;
}

.create-form input {
  background: var(--color-surface, transparent);
  border: 2px solid var(--color-border);
  border-radius: 4px;
  color: var(--color-text);
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: 0.3rem;
  text-transform: uppercase;
  text-align: center;
  width: 180px;
  padding: 8px;
  outline: none;
}

.create-form input:focus {
  border-color: var(--color-text-muted);
}

.create-form .btn {
  width: auto;
  padding: 12px 24px;
}
