/*
  V26 Release Candidate layout.
  Goal: public mobile-first build with a stable four-band game screen:
  top menu, crossword grid, one-line clue, keyboard.
*/

.app-shell {
  width: min(100%, 500px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(3.85rem + var(--safe-top)) 0.35rem calc(0.55rem + var(--safe-bottom));
}

.app-main {
  position: relative;
  min-height: calc(100dvh - 4.5rem - var(--safe-top));
  overflow: hidden;
  border: 1px solid rgba(163, 111, 50, 0.42);
  border-radius: 28px 28px 18px 18px;
  background: linear-gradient(180deg, rgba(255, 250, 239, 0.98), rgba(247, 232, 197, 0.98));
  box-shadow: var(--shadow);
}

.app-main::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at top right, rgba(255, 255, 255, 0.45), transparent 30%);
}

.screen {
  position: relative;
  padding: 0.9rem;
}

.screen[hidden] {
  display: none !important;
}

/* Top menu */
.app-topbar {
  position: fixed;
  top: 0;
  left: 50%;
  z-index: 100;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  align-items: center;
  gap: 0.25rem;
  width: min(100%, 500px);
  min-height: calc(3.35rem + var(--safe-top));
  padding: calc(0.35rem + var(--safe-top)) 0.55rem 0.35rem;
  transform: translateX(-50%);
  border-bottom: 1px solid rgba(122, 85, 32, 0.24);
  background: linear-gradient(180deg, rgba(255, 247, 226, 0.99), rgba(240, 201, 135, 0.98));
  box-shadow: 0 8px 22px rgba(76, 52, 30, 0.16);
  backdrop-filter: blur(12px);
}

.topbar-title {
  display: inline-flex;
  grid-column: 2;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-width: 0;
  color: #5e421d;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.08rem;
  letter-spacing: -0.02em;
}

.topbar-app-icon,
.topbar-home-button img {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  box-shadow: 0 2px 7px rgba(76, 52, 30, 0.2);
}

.topbar-home-button,
.topbar-menu-button,
.options-close-button {
  display: inline-grid;
  place-items: center;
  width: 42px;
  min-width: 42px;
  min-height: 42px;
  border: 1px solid rgba(122, 85, 32, 0.26);
  border-radius: 999px;
  background: rgba(255, 250, 237, 0.8);
  color: #6f4f18;
  font-size: 1.25rem;
  font-weight: 950;
  line-height: 1;
}

.topbar-home-button { grid-column: 1; }
.topbar-menu-button { grid-column: 3; }
.topbar-home-button:active,
.topbar-menu-button:active,
.options-close-button:active { transform: translateY(1px); }

/* Options drawer */
.options-backdrop {
  position: fixed;
  inset: calc(3.35rem + var(--safe-top)) 0 0;
  z-index: 105;
  background: rgba(67, 45, 22, 0.34);
  backdrop-filter: blur(2px);
}

.options-backdrop[hidden],
.options-panel[hidden] { display: none !important; }

.options-panel {
  position: fixed;
  top: calc(3.55rem + var(--safe-top));
  right: max(0.5rem, calc((100vw - 500px) / 2 + 0.5rem));
  z-index: 110;
  width: min(360px, calc(100vw - 1rem));
  max-height: calc(100dvh - 4.25rem - var(--safe-top));
  overflow: auto;
  padding: 0.9rem;
  border: 1px solid rgba(122, 85, 32, 0.24);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(255, 250, 239, 0.99), rgba(247, 232, 197, 0.99));
  box-shadow: 0 24px 58px rgba(76, 52, 30, 0.28);
}

.options-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.8rem;
}

.options-panel-head h2 { margin: 0; color: #5e421d; font-size: 1.35rem; }

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
  margin: 0.95rem 0 1rem;
}

.options-panel .filters { grid-template-columns: 1fr; margin: 0 0 0.85rem; }

.filters label {
  display: grid;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 850;
}

.filters label:last-child { grid-column: 1 / -1; }
.options-panel .filters label:last-child { grid-column: auto; }

.filters select {
  width: 100%;
  min-height: 46px;
  padding: 0.65rem 0.8rem;
  border: 1px solid rgba(180, 137, 79, 0.65);
  border-radius: 14px;
  background: rgba(255, 251, 242, 0.96);
  color: var(--text);
}

.library-controls { margin: 0 0 0.9rem; }
.options-panel .library-controls { margin: 0; }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-height: 44px;
  padding: 0.75rem 0.85rem;
  border: 1px solid rgba(180, 137, 79, 0.55);
  border-radius: 16px;
  background: rgba(255, 251, 242, 0.95);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 850;
}

.toggle-row input { width: 22px; height: 22px; accent-color: var(--accent); }

/* Home/list */
.story-intro,
.quick-actions,
.library-stats,
.crossword-card,
.list-message {
  border: 1px solid rgba(180, 137, 79, 0.5);
  background: rgba(255, 251, 242, 0.95);
}

.story-intro {
  margin-bottom: 0.85rem;
  padding: 0.95rem;
  border-radius: 22px;
}

.section-head,
.game-topbar { display: grid; gap: 0.7rem; margin-bottom: 1rem; }

.quick-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.7rem;
  margin: 0.2rem 0 1rem;
  padding: 0.85rem;
  border-radius: var(--radius);
}

.quick-actions .quick-action-note { grid-column: 1 / -1; }
.quick-action-note { margin: 0; color: var(--muted); font-size: 0.9rem; font-weight: 750; }
.quick-action-note[data-type="error"] { color: var(--wrong); }
.quick-action-note[data-type="partial"] { color: #9a5c18; }

.library-stats {
  margin: -0.2rem 0 0.95rem;
  padding: 0.7rem 0.8rem;
  border-radius: 14px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.card-grid { display: grid; grid-template-columns: 1fr; gap: 0.85rem; }
.crossword-card { display: grid; gap: 0.65rem; padding: 1rem; border-radius: var(--radius); box-shadow: 0 10px 24px rgba(107, 77, 31, 0.09); }
.crossword-card h3 { margin: 0; font-size: 1.18rem; }
.crossword-card p { margin: 0; }
.screen-list .crossword-card > div > p { display: none; }

.card-meta { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0.3rem 0.65rem;
  border: 1px solid rgba(38, 77, 120, 0.12);
  border-radius: 999px;
  background: #edf4fb;
  color: #264d78;
  font-size: 0.75rem;
  font-weight: 850;
}
.card-progress { display: grid; gap: 0.35rem; color: var(--muted); font-size: 0.85rem; }

/* Shared buttons */
.primary-button,
.secondary-button,
.ghost-button,
.danger-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 0.75rem 0.95rem;
  border-radius: 14px;
  font-weight: 850;
}
.primary-button { background: linear-gradient(180deg, #335e90, #264d78); color: #fff8eb; }
.secondary-button { background: linear-gradient(180deg, #f5df9b, #efc96c); color: #6f4f18; }
.ghost-button { border: 1px solid rgba(255,255,255,0.42); background: rgba(255,250,239,0.18); color: #fff7e4; }
.danger-button { background: #f9e0dc; color: var(--wrong); }
.primary-button:disabled,
.secondary-button:disabled,
.ghost-button:disabled,
.danger-button:disabled { cursor: not-allowed; opacity: 0.52; }

.list-message {
  margin: 0;
  padding: 1rem;
  border-radius: var(--radius);
  font-weight: 750;
}
.list-message.compact { padding: 0.7rem 0.85rem; font-size: 0.86rem; }
.list-message[data-type="partial"] { border-color: #efcf7e; background: #fff7d8; color: #8f5c17; }
.list-message[data-type="error"],
.status-message[data-type="error"],
.status-message[data-type="wrong"] { color: var(--wrong); }
.status-message[data-type="correct"] { color: var(--correct); }
.status-message[data-type="partial"] { color: #9a5c18; }

/* Game desktop/tablet fallback */
.game-layout,
.clues-panel { display: grid; grid-template-columns: 1fr; }
.game-layout { gap: 0.7rem; }
.crossword-panel,
.clues-panel { border: 1px solid rgba(180, 137, 79, 0.46); border-radius: 18px; background: rgba(255, 250, 241, 0.92); }
.crossword-panel { position: relative; padding: 0.45rem; overflow: auto; -webkit-overflow-scrolling: touch; }
.clues-panel { gap: 1rem; max-height: 34dvh; padding: 0.8rem; overflow: auto; -webkit-overflow-scrolling: touch; }
.command-bar { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.6rem; margin: 0.9rem 0 0; }
.status-message { min-height: 1.45rem; margin: 0.75rem 0 0; font-weight: 750; }
.progress-wrap { display: grid; gap: 0.35rem; min-width: 140px; color: var(--muted); font-weight: 800; text-align: right; }
.progress-bar { height: 10px; border-radius: 999px; background: rgba(38, 77, 120, 0.15); overflow: hidden; }
.progress-bar > div { height: 100%; width: 0%; border-radius: inherit; background: linear-gradient(90deg, #efb54a, #264d78); transition: width 160ms ease; }

/* Compact game: topbar + grid + one-line clue + keyboard */
body.is-playing { overflow: hidden; }
body.is-playing .app-shell {
  width: 100%;
  max-width: 500px;
  height: 100dvh;
  min-height: 100dvh;
  padding: calc(3.55rem + var(--safe-top)) 0.2rem 0;
}
body.is-playing .app-main {
  height: calc(100dvh - 3.55rem - var(--safe-top));
  min-height: calc(100dvh - 3.55rem - var(--safe-top));
  border-radius: 18px 18px 0 0;
  overflow: hidden;
}
body.is-playing.has-app-keyboard .screen-game {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  height: calc(100dvh - 3.55rem - var(--safe-top));
  min-height: calc(100dvh - 3.55rem - var(--safe-top));
  padding: calc(0.15rem + var(--safe-top)) 0.22rem calc(var(--keyboard-height, 9.8rem) + var(--safe-bottom) + 0.16rem);
  overflow: hidden;
  overscroll-behavior: contain;
}
body.is-playing.has-app-keyboard .game-topbar,
body.is-playing.has-app-keyboard .status-message,
body.is-playing.has-app-keyboard .command-bar,
body.is-playing.has-app-keyboard .clues-panel { display: none !important; }
body.is-playing.has-app-keyboard .play-viewport {
  display: grid;
  min-height: 0;
  align-content: center;
  justify-items: center;
}
body.is-playing.has-app-keyboard .game-layout {
  display: grid;
  width: 100%;
  min-height: 0;
  align-items: center;
}
body.is-playing.has-app-keyboard .crossword-panel {
  display: grid;
  place-items: center;
  width: 100%;
  max-height: calc(100dvh - var(--keyboard-height, 9.8rem) - 5.7rem - var(--safe-top) - var(--safe-bottom));
  padding: 0.1rem;
  border: 0;
  border-radius: 8px;
  background: transparent;
  overflow: hidden;
}
.active-clue-panel {
  display: grid;
  gap: 0.25rem;
  margin: 0 0 0.75rem;
  padding: 0.8rem;
  border: 1px solid rgba(215, 180, 123, 0.8);
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(255, 249, 232, 0.98), rgba(244, 228, 194, 0.95));
  box-shadow: 0 8px 20px rgba(140, 99, 43, 0.08);
  cursor: pointer;
  user-select: none;
}
.active-clue-panel p { margin: 0; color: #29435f; font-size: 0.96rem; font-weight: 850; }
.active-clue-panel:focus-visible { outline: 3px solid rgba(38, 77, 120, 0.3); outline-offset: 2px; }
body.is-playing.has-app-keyboard .active-clue-panel {
  position: relative;
  align-self: end;
  min-height: 1.85rem;
  max-height: 2.05rem;
  margin: 0.12rem 0 0;
  padding: 0.28rem 0.48rem;
  border-radius: 10px;
  box-shadow: 0 5px 14px rgba(140, 99, 43, 0.12);
}
body.is-playing.has-app-keyboard .active-clue-panel p {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: clamp(0.78rem, 3.2vw, 0.94rem);
  line-height: 1.08;
}

/* Keyboard */
.app-keyboard {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  width: min(100%, 500px);
  margin: 0 auto;
  padding: 0.36rem 0.32rem calc(0.42rem + var(--safe-bottom));
  border-top: 1px solid rgba(140, 99, 43, 0.34);
  background: linear-gradient(180deg, #9a6437, #7f4f2a);
  box-shadow: 0 -14px 30px rgba(90, 58, 25, 0.38);
}
.app-keyboard[hidden],
body:not(.has-app-keyboard) .app-keyboard { display: none !important; }
.keyboard-rows { display: grid; gap: 0.24rem; }
.keyboard-row { display: flex; justify-content: center; gap: 0.18rem; }
.keyboard-key {
  flex: 1 1 0;
  min-width: 0;
  min-height: clamp(38px, 8.8vw, 46px);
  border: 1px solid rgba(88, 55, 25, 0.32);
  border-radius: 10px;
  background: #fff9ea;
  color: #3d3320;
  font-size: clamp(0.82rem, 3.35vw, 1rem);
  font-weight: 950;
  box-shadow: 0 2px 0 rgba(76, 52, 30, 0.38);
}
.keyboard-key:active { transform: translateY(1px); box-shadow: none; }
.keyboard-key-wide { flex: 1.8 1 0; font-size: clamp(0.78rem, 3.05vw, 0.95rem); }
.keyboard-key-delete { background: #f8ead2; color: #6b4c24; }
.keyboard-key-clear { background: #dcecfb; color: #1f466d; }
.keyboard-key-help {
  flex: 1.1 1 0;
  min-width: 3.8rem;
  background: #fff2b8;
  color: #60440f;
  font-weight: 950;
}
.keyboard-key-help.is-help-ready {
  outline: 2px solid rgba(239, 181, 74, 0.85);
  outline-offset: -2px;
  transform: translateY(-1px);
}
.keyboard-key-help:disabled { opacity: 0.38; filter: grayscale(0.35); }
.crossword-cell.fx-help-letter {
  animation: help-letter-pulse 900ms ease-out;
}
@keyframes help-letter-pulse {
  0% { box-shadow: 0 0 0 0 rgba(239, 181, 74, 0.9); transform: scale(1); }
  35% { box-shadow: 0 0 0 6px rgba(239, 181, 74, 0.18); transform: scale(1.08); }
  100% { box-shadow: 0 0 0 0 rgba(239, 181, 74, 0); transform: scale(1); }
}

/* Dialogs */
.completion-overlay,
.clue-popup {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: calc(4rem + var(--safe-top)) 1rem 1rem;
  background: rgba(67, 45, 22, 0.55);
}
.completion-overlay[hidden],
.clue-popup[hidden] { display: none !important; }
.completion-card,
.clue-popup-card {
  display: grid;
  gap: 0.8rem;
  width: min(100%, 420px);
  padding: 1.05rem;
  border: 1px solid rgba(180, 137, 79, 0.45);
  border-radius: 24px;
  background: linear-gradient(180deg, #fffaf0, #f7e9c9);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(67, 45, 22, 0.25);
}
.completion-kicker,
.clue-popup-kicker { margin: 0; color: #264d78; font-size: 0.72rem; font-weight: 950; letter-spacing: 0.08em; text-transform: uppercase; }
.completion-card h3,
.completion-card p,
.clue-popup-card h3,
.clue-popup-card p { margin: 0; }
.clue-popup-card #cluePopupText { color: #243452; font-size: 1rem; font-weight: 850; line-height: 1.25; }
.completion-actions { display: grid; grid-template-columns: 1fr; gap: 0.55rem; }
body.has-options-panel .completion-overlay,
body.has-options-panel .clue-popup { pointer-events: none; }

@media (max-width: 430px) {
  .quick-actions { grid-template-columns: 1fr; }
  .quick-actions .primary-button,
  .quick-actions .secondary-button { width: 100%; }
  .card-grid { gap: 0.75rem; }
  .crossword-card { padding: 0.85rem; }
}

@media (max-width: 390px) {
  .app-shell { padding-left: 0.22rem; padding-right: 0.22rem; }
  .screen { padding: 0.68rem; }
  body.is-playing.has-app-keyboard .screen-game { padding-left: 0.16rem; padding-right: 0.16rem; }
  .keyboard-row { gap: 0.14rem; }
  .keyboard-key { min-height: clamp(36px, 9.6vw, 43px); border-radius: 9px; }
}

@media (max-width: 360px) {
  .filters { grid-template-columns: 1fr; }
  .filters label:last-child { grid-column: auto; }
  .keyboard-key { font-size: 0.78rem; }
  .keyboard-key-help { min-width: 3.4rem; }
}
