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

:root {
  /* Match main map ocean / water */
  --water: #6fa8c9;
  --bg: #0c0e14;
  --surface: #161923;
  --surface2: #1e2230;
  --border: #2a2e3e;
  --text: #e2e4ec;
  --text-dim: #7a7e94;
  --text-on-water: #000;
  --accent: #6c8cff;
  --accent-glow: rgba(108, 140, 255, 0.25);
  --green: #3ddba0;
  --orange: #f0a050;
  --red: #f06068;
  --purple: #b080f0;
  --radius: 8px;
  --font-body: 'DM Sans', system-ui, sans-serif;
  --font-mono: 'DM Mono', 'Consolas', monospace;
}

html {
  font-size: 15px;
  height: 100%;
  background: var(--water);
}

body {
  font-family: var(--font-body);
  background: var(--water);
  color: var(--text-on-water);
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

#game {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background: var(--water);
}

/* ================================================================== */
/*  Loading                                                            */
/* ================================================================== */

#loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--water);
  z-index: 100;
  transition: opacity 0.4s;
}

#loading.fade-out { opacity: 0; pointer-events: none; }

.loader-content { text-align: center; }

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}

@keyframes spin { to { transform: rotate(360deg); } }

#loading-msg {
  color: var(--text-on-water);
  font-size: 0.85rem;
}

/* ================================================================== */
/*  Header                                                             */
/* ================================================================== */

#header {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.6rem;
  padding: 0.8rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  color: var(--text);
  overflow: visible;
  z-index: 6;
  --guess-col: 220px;
}

.title-home {
  color: inherit;
  text-decoration: none;
}
.title-home:hover { color: var(--accent); }

.header-left {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
  grid-column: 1;
}
.header-brand-home {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 999px;
  line-height: 0;
}
.header-brand-home:hover,
.header-brand-home:focus-visible {
  outline: none;
  transform: translateY(-1px);
}
.header-brand-home-svg {
  display: block;
  border-radius: 999px;
}
.header-game-name {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--accent);
  white-space: nowrap;
}

.header-icon-cluster {
  display: flex;
  align-items: center;
  gap: 17px;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 6px;
  border: 2px solid var(--accent);
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  border-radius: 8px;
  line-height: 0;
}
.icon-btn:hover {
  background: var(--accent-glow);
  border-color: var(--accent);
  color: var(--accent);
}
.icon-btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.icon-btn:disabled:hover {
  background: transparent;
}
.icon-btn svg {
  display: block;
}

.hidden { display: none !important; }

.panel-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(8, 12, 22, 0.55);
  display: grid;
  place-items: center;
  padding: 1rem;
}
.panel-overlay.hidden { display: none !important; }
.panel-card {
  width: min(380px, 100%);
  max-height: min(90vh, 640px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem 1.1rem 1.15rem;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  color: var(--text);
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.panel-header h2 {
  margin: 0;
  font-size: 1.15rem;
}
.panel-close {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.1rem 0.35rem;
}
.panel-sub {
  margin: 0.35rem 0 0.85rem;
  color: var(--text-dim);
  font-size: 0.85rem;
}
.panel-hint {
  margin: 0.75rem 0 0;
  color: var(--text-dim);
  font-size: 0.75rem;
}
.calendar-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.55rem;
}
.calendar-month-label {
  font-weight: 600;
  font-size: 0.95rem;
}
.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.2rem;
  margin-bottom: 0.25rem;
  text-align: center;
  font-size: 0.65rem;
  color: var(--text-dim);
  text-transform: uppercase;
}
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0.25rem;
}
.calendar-day {
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0;
}
.calendar-day.other {
  border: none;
  background: transparent;
  cursor: default;
}
.calendar-day.today {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.calendar-day.won {
  background: rgba(80, 180, 120, 0.35);
  border-color: rgba(80, 180, 120, 0.55);
}
.calendar-day.incomplete {
  background: rgba(230, 180, 70, 0.28);
  border-color: rgba(230, 180, 70, 0.5);
}
.calendar-day.future {
  opacity: 0.35;
  cursor: default;
}
.calendar-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  margin-top: 0.75rem;
  font-size: 0.72rem;
  color: var(--text-dim);
}
.calendar-legend i.leg {
  display: inline-block;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 3px;
  margin-right: 0.25rem;
  vertical-align: -1px;
  border: 1px solid var(--border);
}
.calendar-legend i.won { background: rgba(80, 180, 120, 0.45); }
.calendar-legend i.incomplete { background: rgba(230, 180, 70, 0.4); }
.calendar-legend i.today { outline: 2px solid var(--accent); outline-offset: -2px; }

.stats-filters {
  display: grid;
  gap: 0.45rem;
  margin: 0.75rem 0 1rem;
}
.stats-filters label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--text-dim);
}
.stats-filters select {
  min-width: 8rem;
}
.stats-grid {
  margin: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}
.stats-grid > div {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.55rem 0.65rem;
}
.stats-grid dt {
  margin: 0;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
}
.stats-grid dd {
  margin: 0.15rem 0 0;
  font-family: var(--font-mono);
  font-size: 1.05rem;
  font-weight: 600;
}

.win-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 1rem;
}
.btn-quiet, .file-btn {
  font: inherit;
  font-size: 0.85rem;
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
}
.file-btn { display: inline-block; }

h1 {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--accent);
}

.header-center {
  position: absolute;
  inset: 0;
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 1;
}

.header-left,
.header-right {
  position: relative;
  z-index: 2;
}

.header-target-wrap {
  position: relative;
  width: var(--guess-col);
  min-width: 0;
  text-align: center;
  pointer-events: auto;
}

.header-target {
  width: 100%;
  min-width: 0;
  text-align: center;
}

[data-header-live-home] {
  display: contents;
}

.target-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  margin-bottom: 0.15rem;
}

.target-word {
  font-family: var(--font-mono);
  font-size: 1.4rem;
  font-weight: 500;
  color: var(--green);
  letter-spacing: 0.08em;
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.7rem;
  flex-wrap: nowrap;
  grid-column: 3;
  pointer-events: none;
}

.header-right > * {
  pointer-events: auto;
}

.header-right .toolbar-mode {
  margin-left: 0;
}

.header-right .header-def-field {
  display: flex;
  align-items: center;
}

.header-right .language-dropdown-list {
  left: auto;
  right: 0;
}

.header-def-menu .header-def-field {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  flex: 1 1 100%;
}

.header-optional-cluster {
  position: absolute;
  right: 50%;
  margin-right: calc(var(--guess-col) / 2 + 0.7rem);
  display: flex;
  align-items: center;
  gap: 0.7rem;
  pointer-events: auto;
}

.header-optional,
.play-scoreboard {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.play-scoreboard {
  position: absolute;
  left: 50%;
  margin-left: calc(var(--guess-col) / 2 + 0.7rem);
  pointer-events: auto;
}

.stat { text-align: center; }
.stat--hidden { display: none !important; }

.header-optional .stat-label,
.play-scoreboard .stat-label {
  display: block;
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  white-space: nowrap;
}

.header-optional .stat-value,
.play-scoreboard .stat-value {
  font-family: var(--font-mono);
  font-size: 1.1rem;
  font-weight: 500;
}

.header-chrome-tight .header-optional .stat,
.header-chrome-tight .play-scoreboard .stat {
  position: relative;
}

.header-chrome-tight .header-optional .stat-label,
.header-chrome-tight .play-scoreboard .stat-label,
.header-chrome-tight .target-label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.header-chrome-tight .header-optional .stat-value,
.header-chrome-tight .play-scoreboard .stat-value {
  font-size: 0.78rem;
}

.header-chrome-tight .header-optional-cluster,
.header-chrome-tight .play-scoreboard {
  gap: 0.35rem;
}

.header-chrome-tight .header-right {
  gap: 0.3rem;
}

.header-chrome-tight .header-right select {
  field-sizing: content;
  max-width: 3.8rem;
  padding: 0.28rem 0.95rem 0.28rem 0.22rem;
  font-size: 0.68rem;
}

.header-live-tip {
  display: none;
}

.header-chrome-stats-tip .header-target-wrap {
  cursor: help;
}

.header-chrome-stats-tip .header-live-tip {
  display: none;
  position: absolute;
  left: 50%;
  top: calc(100% + 6px);
  transform: translateX(-50%);
  flex-direction: row;
  align-items: center;
  gap: 0.7rem;
  padding: 0.45rem 0.7rem;
  background: var(--surface, #161923);
  border: 1px solid var(--border, #2a2e3e);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 80;
  pointer-events: none;
  white-space: nowrap;
}

.header-chrome-stats-tip .header-target-wrap:hover .header-live-tip,
.header-chrome-stats-tip .header-target-wrap.is-tip-open .header-live-tip,
.header-chrome-stats-tip .header-target-wrap:focus-within .header-live-tip {
  display: flex;
}

.header-chrome-stats-tip .header-live-tip .header-optional,
.header-chrome-stats-tip .header-live-tip .play-scoreboard {
  position: static;
  margin: 0;
  gap: 0.7rem;
}

.header-chrome-stats-tip .header-live-tip .stat-label,
.header-chrome-stats-tip .header-live-tip .header-optional .stat-label,
.header-chrome-stats-tip .header-live-tip .play-scoreboard .stat-label {
  position: static;
  width: auto;
  height: auto;
  overflow: visible;
  clip: auto;
  display: block;
  font-size: 0.6rem;
}

.header-chrome-stats-tip .header-live-tip .stat-value,
.header-chrome-stats-tip .header-live-tip .header-optional .stat-value,
.header-chrome-stats-tip .header-live-tip .play-scoreboard .stat-value {
  font-size: 1.05rem;
}

/* ================================================================== */
/*  Chain                                                              */
/* ================================================================== */

#chain-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  padding: 2rem 1rem 1rem;
}

#chain {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  width: 100%;
  max-width: 340px;
}

/* -- Word node ---------------------------------------------------- */

.node {
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: nodeIn 0.3s ease-out both;
}

@keyframes nodeIn {
  from { opacity: 0; transform: translateY(-12px); }
  to   { opacity: 1; transform: translateY(0); }
}

.word-slot {
  width: 220px;
  padding: 0.7rem 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
  font-family: var(--font-mono);
  font-size: 1.25rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--text);
  position: relative;
}

/* -- Letter highlights -------------------------------------------- */

.letter-green  { color: #3ddba0; }
.letter-yellow { color: #f0c050; }
.letter-red    { color: #f07070; }

.word-slot.start-word {
  border-color: var(--accent);
  box-shadow: 0 0 12px var(--accent-glow);
}

.word-slot.end-word {
  border-color: var(--green);
  box-shadow: 0 0 12px rgba(61, 219, 160, 0.2);
}

.word-slot.deadend-word {
  border-color: var(--red);
  box-shadow: 0 0 12px rgba(240, 96, 104, 0.25);
}

/* -- Undo X on last word ----------------------------------------- */

.word-slot-wrap {
  position: relative;
  display: inline-block;
}

.advanced-star {
  position: absolute;
  z-index: 2;
  left: 0.4rem;
  top: 50%;
  transform: translateY(-50%);
  color: #f0c050;
  font-size: 1.05rem;
  line-height: 1;
  cursor: help;
}

.missing-word-report {
  position: absolute;
  z-index: 3;
  top: 50%;
  left: calc(100% + 8px);
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.missing-word-report.is-above {
  left: 50%;
  right: auto;
  top: auto;
  bottom: calc(100% + 6px);
  transform: translateX(-50%);
}

.missing-word-report img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  pointer-events: none;
}

.missing-word-report[hidden] {
  display: none;
}

.undo-x {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--orange);
  border: 2px solid var(--bg);
  color: #000;
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
  z-index: 2;
  padding: 0;
  font-family: var(--font-body);
}

.undo-x:hover {
  transform: scale(1.2);
  background: #e0903a;
  border-color: var(--bg);
}

.help-x {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s, opacity 0.15s;
  z-index: 2;
  padding: 0;
  font-family: var(--font-body);
}

.help-x:hover:not(:disabled) {
  transform: scale(1.2);
}

.help-x:disabled {
  opacity: 0.4;
  cursor: default;
}

/* -- Connector ---------------------------------------------------- */

.connector {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 48px;
  justify-content: center;
  position: relative;
  animation: nodeIn 0.3s ease-out both;
}

.connector-line {
  width: 2px;
  flex: 1;
  background: var(--border);
}

.op-badge {
  position: absolute;
  right: -90px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.7rem;
  font-weight: 500;
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  white-space: nowrap;
  letter-spacing: 0.03em;
}

.op-badge.substitute { background: var(--orange); color: #000; }
.op-badge.insert     { background: var(--green);  color: #000; }
.op-badge.delete     { background: var(--red);    color: #fff; }
.op-badge.anagram    { background: var(--purple); color: #fff; }

/* -- Input node --------------------------------------------------- */

.input-slot {
  width: 220px;
  padding: 0.65rem 1rem;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  background: var(--bg);
  text-align: center;
  font-family: var(--font-mono);
  font-size: 1.25rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  color: var(--text);
  outline: none;
  caret-color: var(--accent);
  box-shadow: 0 0 16px var(--accent-glow);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.input-slot::placeholder {
  color: var(--text-dim);
  font-weight: 400;
  letter-spacing: 0.02em;
}

.input-slot:focus {
  border-color: var(--accent);
  box-shadow: 0 0 24px var(--accent-glow);
}

@media (pointer: coarse) {
  .input-slot {
    caret-color: transparent;
    user-select: none;
    -webkit-user-select: none;
  }
}

/* -- Shake -------------------------------------------------------- */

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15%  { transform: translateX(-8px); }
  30%  { transform: translateX(7px); }
  45%  { transform: translateX(-6px); }
  60%  { transform: translateX(5px); }
  75%  { transform: translateX(-3px); }
  90%  { transform: translateX(2px); }
}

.shake {
  animation: shake 0.45s ease-out;
  border-color: var(--red) !important;
  box-shadow: 0 0 16px rgba(240, 96, 104, 0.3) !important;
}

/* -- Error hint --------------------------------------------------- */

.error-hint {
  /* The longest message must fit within the 220px input box. */
  max-width: 220px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.2;
  color: #e00000;
  margin-top: 0.35rem;
  min-height: 1.1rem;
  opacity: 0;
  transition: opacity 0.2s;
}

.error-hint.visible { opacity: 1; }

.deadend-banner {
  flex-shrink: 0;
  padding: 0.45rem 1rem;
  text-align: center;
  font-size: 0.82rem;
  font-weight: 500;
  color: #fff;
  background: var(--red);
}

/* ================================================================== */
/*  Toolbar                                                            */
/* ================================================================== */

#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.55rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  color: var(--text);
  position: relative;
  z-index: 5;
  overflow: visible;
}

.toolbar-picks,
.toolbar-mode {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}

.toolbar-mode {
  margin-left: auto;
}

.mode-dropdown {
  position: relative;
  z-index: 50;
}

.mode-select,
.mode-dropdown-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  width: auto;
  min-width: 5.5rem;
  height: 32px;
  padding: 0.35rem 0.55rem;
  font-family: var(--font-body);
  font-size: 0.82rem;
  font-weight: 500;
}

.mode-dropdown-list {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 100%;
  width: max-content;
  margin: 4px 0 0;
  padding: 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  z-index: 40;
  color: var(--text);
}

.mode-select:hover:not(:disabled),
.mode-dropdown-trigger:hover:not(:disabled) {
  background: var(--surface2);
  border-color: var(--accent);
  color: var(--text);
}

.mode-select:focus,
.mode-dropdown-trigger:focus {
  outline: none;
  border-color: var(--accent);
}

.date-picker-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  width: 32px;
  height: 32px;
  padding: 0;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.date-picker-btn svg {
  flex-shrink: 0;
  color: var(--text-dim);
}
.date-picker-btn.is-today svg {
  color: var(--accent);
}
#date-display {
  display: none;
  font-size: 0.82rem;
  font-weight: 500;
}

.language-dropdown {
  position: relative;
}
.language-dropdown-trigger {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  height: 32px;
  padding: 0 0.4rem 0 0.45rem;
  min-width: 0;
}
.language-dropdown-flag {
  font-size: 1.15rem;
  line-height: 1;
}
.language-dropdown-chevron {
  font-size: 0.55rem;
  color: var(--text-dim);
}
.language-dropdown-list {
  position: absolute;
  top: calc(100% + 4px);
  bottom: auto;
  left: 0;
  min-width: 10rem;
  width: max-content;
  max-width: min(280px, 80vw);
  margin: 0;
  padding: 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 40;
  max-height: 240px;
  overflow-y: auto;
  color: var(--text);
}
.language-dropdown-option {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.65rem;
  cursor: pointer;
  font-size: 0.85rem;
}
.language-dropdown-option:hover,
.language-dropdown-option.selected {
  background: var(--surface2);
}
.language-dropdown-option-flag {
  font-size: 1.1rem;
  flex-shrink: 0;
}
.language-dropdown-option-name {
  white-space: nowrap;
}

.display-prefs {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.85rem 1.1rem;
  margin: 0.7rem 0 0;
}
.pref-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.88rem;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  line-height: 1.15;
}
.pref-toggle input {
  accent-color: var(--green, #6fa86f);
  margin: 0;
}

.display-popover {
  position: fixed;
  z-index: 80;
  width: max-content;
  min-width: 11.5rem;
  padding: 0.65rem 0.8rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  color: var(--text);
}
.display-popover.hidden { display: none !important; }
.display-popover .panel-header h2 {
  font-size: 1rem;
}
.display-popover .display-prefs {
  flex-direction: column;
  align-items: flex-start;
  gap: 0.45rem;
  margin: 0.5rem 0 0;
}

#keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  padding: 0.7rem 0.6rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  color: var(--text);
}

button {
  font-family: var(--font-body);
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.45rem 1.1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

button:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button:disabled {
  opacity: 0.35;
  cursor: default;
}

.toolbar-right {
  display: none;
}

select {
  font-family: var(--font-body);
  font-size: 0.82rem;
  padding: 0.35rem 0.6rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
}

select:focus { outline: none; border-color: var(--accent); }

.keyboard-row {
  display: flex;
  gap: 6px;
  justify-content: center;
  flex-wrap: nowrap;
  width: 100%;
  max-width: 100%;
}

.keyboard-spacer {
  min-width: 40px;
  height: 44px;
  flex-shrink: 0;
}

.key {
  flex: 1 1 0;
  min-width: 0;
  max-width: 42px;
  height: 42px;
  border: none;
  border-radius: 6px;
  background: #818384;
  color: #fff;
  font-family: var(--font-body);
  font-size: clamp(0.58rem, calc(70vw / var(--kb-max-keys, 10)), 0.9rem);
  font-weight: 600;
  cursor: pointer;
  text-transform: uppercase;
  padding: 0 2px;
}

.key:hover {
  background: #6a6c6e;
}

.key.key-action {
  flex: 1.45 1 0;
  min-width: 0;
  max-width: 72px;
  font-size: clamp(0.5rem, calc(52vw / var(--kb-max-keys, 10)), 0.72rem);
}

/* ================================================================== */
/*  Win overlay                                                        */
/* ================================================================== */

#win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(12, 14, 20, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.win-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2.5rem 3rem;
  text-align: center;
  animation: popIn 0.35s ease-out;
  max-width: 420px;
  width: 90%;
  color: var(--text);
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.9); }
  to   { opacity: 1; transform: scale(1); }
}

.win-title {
  font-size: 2rem;
  font-weight: 700;
  color: var(--green);
  margin-bottom: 1.5rem;
  letter-spacing: -0.02em;
}

.win-stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1.25rem 1.75rem;
  margin-bottom: 1.5rem;
}

.win-stat { text-align: center; }

.win-num {
  display: block;
  font-family: var(--font-mono);
  font-size: 1.6rem;
  font-weight: 500;
  color: var(--text);
}

.win-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.win-solution {
  margin-bottom: 1.5rem;
}

.win-solution .win-label {
  display: block;
  margin-bottom: 0.35rem;
}

.win-path {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text-dim);
  line-height: 2;
  max-height: min(38vh, 14rem);
  overflow: auto;
  word-break: break-word;
}

.win-path-word {
  color: var(--text);
  font-weight: 600;
}

.win-path .op-inline {
  display: inline-block;
  font-size: 0.6rem;
  font-weight: 600;
  padding: 0.1rem 0.35rem;
  border-radius: 6px;
  margin: 0 0.15rem;
  vertical-align: middle;
}

.win-path .op-inline.substitute { background: var(--orange); color: #000; }
.win-path .op-inline.insert     { background: var(--green);  color: #000; }
.win-path .op-inline.delete     { background: var(--red);    color: #fff; }
.win-path .op-inline.anagram    { background: var(--purple); color: #fff; }

/* ================================================================== */
/*  Scrollbar                                                          */
/* ================================================================== */

#chain-container::-webkit-scrollbar { width: 5px; }
#chain-container::-webkit-scrollbar-track { background: transparent; }
#chain-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ================================================================== */
/*  Responsive                                                         */
/* ================================================================== */

html, body {
  min-height: 100dvh;
}

body {
  padding:
    env(safe-area-inset-top, 0)
    env(safe-area-inset-right, 0)
    env(safe-area-inset-bottom, 0)
    env(safe-area-inset-left, 0);
}

#keyboard {
  overflow-x: hidden;
  max-width: 100%;
}

@media (max-width: 720px) {
  #header {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) max-content;
    grid-template-areas:
      "left right"
      "center center";
    align-items: center;
    gap: 0.2rem 0.35rem;
    padding: 0.4rem 0.55rem 0.45rem;
    --guess-col: min(8.5rem, calc(100vw - 16rem));
  }

  .header-left {
    grid-area: left;
    flex: unset;
    min-width: 0;
    z-index: 2;
  }

  .header-right {
    grid-area: right;
    grid-column: auto;
    z-index: 3;
  }

  .header-game-name {
    display: none;
  }

  #header.header-chrome-compact [data-header-chrome="icons"] {
    display: flex !important;
  }

  #header [data-header-chrome="more"],
  #header.header-chrome-compact [data-header-chrome="more"] {
    display: none !important;
  }

  .header-center {
    grid-area: center;
    position: relative;
    inset: auto;
    flex: unset;
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--guess-col) minmax(0, 1fr);
    align-items: center;
    justify-content: stretch;
    gap: 0.35rem;
    width: 100%;
    pointer-events: auto;
    min-width: 0;
  }

  .header-optional-cluster,
  .play-scoreboard {
    position: static;
    inset: auto;
    margin: 0;
    flex: 0 0 auto;
  }

  .header-optional-cluster {
    justify-self: start;
    justify-content: flex-start;
    min-width: 0;
  }

  .play-scoreboard {
    justify-self: end;
    justify-content: flex-end;
    min-width: 0;
  }

  .header-target-wrap {
    flex: none;
    width: var(--guess-col);
    justify-self: center;
  }

  #toolbar {
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.45rem 0.7rem;
    padding: 0.4rem 0.55rem;
  }

  #toolbar .toolbar-mode {
    margin-left: 0;
  }

  .header-optional {
    gap: 0.35rem;
  }

  .header-left .icon-btn,
  .header-left .help-trigger--game {
    width: 28px;
    height: 28px;
    padding: 4px;
  }

  .header-optional .stat-value,
  .play-scoreboard .stat-value {
    font-size: 0.78rem;
  }

  .header-optional-cluster,
  .play-scoreboard {
    gap: 0.35rem;
  }

  .target-word { font-size: 1.15rem; }

  #chain-container {
    padding: 1.1rem 0.65rem 0.75rem;
  }

  .word-slot,
  .input-slot {
    width: min(220px, 78vw);
    font-size: 1.1rem;
  }

  .op-badge {
    right: auto;
    left: calc(100% + 0.35rem);
    max-width: 28vw;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .header-right select {
    field-sizing: content;
    width: auto;
    min-width: 0;
    max-width: none;
    padding: 0.3rem 1.15rem 0.3rem 0.35rem;
    font-size: 0.72rem;
  }

  .language-dropdown-trigger {
    padding: 0 0.28rem 0 0.32rem;
  }

  .date-picker-btn {
    width: 32px;
    padding: 0;
    gap: 0;
  }

  #keyboard {
    padding: 0.4rem 0.22rem 0.55rem;
    gap: 4px;
  }

  .key {
    max-width: none;
    height: 36px;
    padding: 0 1px;
    font-size: clamp(0.52rem, calc(62vw / var(--kb-max-keys, 10)), 0.78rem);
  }

  .key.key-action {
    max-width: none;
    font-size: clamp(0.46rem, calc(46vw / var(--kb-max-keys, 10)), 0.62rem);
  }

  .keyboard-row {
    gap: 2px;
  }

  .keyboard-spacer {
    min-width: 0;
    flex: 0.55 1 0;
    height: 36px;
  }

  .win-card {
    width: min(420px, calc(100vw - 1.5rem));
    padding: 1.5rem 1.25rem;
    margin: 0.75rem;
  }
}

@media (max-width: 420px) {
  h1 { font-size: 1.05rem; }
  .stat-value { font-size: 0.95rem; }
  .word-slot,
  .input-slot {
    width: min(200px, 86vw);
    padding: 0.55rem 0.75rem;
  }
  .error-hint {
    max-width: min(200px, 86vw);
    font-size: 0.75rem;
  }
  .op-badge {
    position: static;
    transform: none;
    margin-top: 0.25rem;
  }
  .connector { height: 40px; }
}

body.investigate .header-icon-cluster,
body.investigate .header-more,
body.investigate .header-optional-cluster,
body.investigate [data-header-live-home],
body.investigate .play-scoreboard,
body.investigate .header-live-tip,
body.investigate .header-right,
body.investigate .win-stats {
  display: none !important;
}

.investigate-error {
  margin: 1.5rem auto;
  max-width: 28rem;
  text-align: center;
  color: var(--text-on-water);
  font-size: 1rem;
  line-height: 1.5;
}
