/* ===== CSS Variables ===== */
:root {
  --bg: #1a1a1a;
  --bg-secondary: #2a2a2a;
  --text: #e0e0e0;
  --text-secondary: #999;
  --accent: #63c2eb;
  --accent-dark: #53b9e6;
  --border: #333;
  --hex-fill: #333;
  --hex-fill-center: #63c2eb;
  --hex-stroke: #444;
  --hex-letter: #e0e0e0;
  --found-bg: #333;
  --success: #4caf50;
  --success-light: #1b3a1b;
  --overlay-bg: rgba(0,0,0,0.7);
  --card-bg: #2a2a2a;
  --shadow: 0 2px 10px rgba(0,0,0,0.3);
  --rank-bar: #444;
  --pangram: #63c2eb;
  --pangram-bg: #b89b1a;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  --found-words-font-size: 17px;
}

[data-theme="light"] {
  --bg: #ffffff;
  --bg-secondary: #f5f5f5;
  --text: #1a1a1a;
  --text-secondary: #666;
  --border: #e0e0e0;
  --hex-fill: #e0e0e0;
  --hex-fill-center: #63c2eb;
  --hex-stroke: #ccc;
  --hex-letter: #1a1a1a;
  --found-bg: #f0f0f0;
  --overlay-bg: rgba(0,0,0,0.5);
  --card-bg: #ffffff;
  --shadow: 0 2px 10px rgba(0,0,0,0.1);
  --rank-bar: #e0e0e0;
  --success-light: #e8f5e9;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

#app {
  width: 100%;
  max-width: 420px;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* ===== Header ===== */
header {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  position: relative;
}

header h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.header-actions {
  display: flex;
  margin-left: auto;
  gap: 4px;
}

/* ===== Main Game Area ===== */
#game-view {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.icon-btn:active {
  background: var(--bg-secondary);
}

/* ===== Score Area ===== */
#score-area {
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.6em 1em;
  font-size: 14px;
}

#current-rank {
  font-size: 1em;
  font-weight: 600;
  min-width: 4.5em;
  text-align: right;
  color: var(--text-secondary);
}

#score-bar-container {
  flex: 1;
  height: 0.7em;
  background: var(--rank-bar);
  border-radius: 0.35em;
  position: relative;
  overflow: visible;
}

#score-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 0.35em;
  width: 0%;
  transition: width 0.3s ease;
}

#score-markers {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
}

.score-marker {
  position: absolute;
  top: -0.25em;
  width: 1.1em;
  height: 1.1em;
  background: var(--rank-bar);
  border: 0.15em solid var(--bg);
  border-radius: 50%;
  transform: translateX(-50%);
  z-index: 1;
}

.score-marker.reached {
  background: var(--accent);
}

#score-value {
  font-size: 1em;
  font-weight: 600;
  min-width: 2.5em;
  text-align: left;
  color: var(--text-secondary);
}

/* ===== Found Words ===== */
#found-words-container {
  padding: 0 16px;
  margin-bottom: 4px;
}

#found-words {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow: hidden;
  white-space: nowrap;
  -webkit-mask-image: linear-gradient(to right, black 90%, transparent 100%);
  mask-image: linear-gradient(to right, black 90%, transparent 100%);
  transition: flex-wrap 0.3s, max-height 0.3s, white-space 0.3s;
}

#found-words.expanded {
  flex-wrap: wrap;
  white-space: normal;
  max-height: 200px;
  overflow-y: auto;
  -webkit-mask-image: none;
  mask-image: none;
}

#found-words .word {
  padding: 4px 10px;
  background: var(--found-bg);
  border-radius: 12px;
  font-size: var(--found-words-font-size, 15px);
  text-transform: capitalize;
}

#found-words a.word {
  text-decoration: none;
  color: inherit;
  cursor: pointer;
}

#found-words a.word:hover {
  opacity: 0.8;
}

#found-words .word.pangram {
  background: var(--pangram-bg);
  font-weight: 600;
  color: #fff;
}

.word-sep {
  opacity: 0.4;
}

#found-words.expanded .word-sep {
  display: none;
}

#btn-found-toggle {
  display: block;
  margin: 4px auto 0;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
}

#btn-found-toggle svg {
  transition: transform 0.2s;
}

#btn-found-toggle.expanded svg {
  transform: rotate(180deg);
}

/* ===== Message ===== */
#message {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 140px;
  text-align: center;
  padding: 10px 20px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 12px;
  background: var(--card-bg);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 50;
  white-space: nowrap;
}

#message.visible {
  opacity: 1;
}

#message.error {
  color: #e53935;
}

#message.success {
  color: var(--success);
}

#message.info {
  color: #e6a817;
}

/* ===== Honeycomb ===== */
#hive {
  display: grid;
  grid-template-rows: 0.3fr auto;
  flex: 1;
  padding: 4px 10px 0;
  touch-action: manipulation;
  position: relative;
  min-height: 0;
  align-content: center;
  -webkit-touch-callout: none;
  user-select: none;
}

#hive svg {
  width: 100%;
  max-width: 360px;
  height: auto;
  max-height: 100%;
  justify-self: center;
}

.hex-cell {
  fill: var(--hex-fill);
  stroke: var(--bg);
  stroke-width: 4;
  cursor: pointer;
  transition: fill 0.1s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.hex-cell.center {
  fill: var(--hex-fill-center);
}

.hex-cell:active {
  fill: var(--accent-dark);
}

.hex-letter {
  font-size: 28px;
  font-weight: 700;
  fill: var(--hex-letter);
  pointer-events: none;
  font-family: var(--font);
}

.hex-letter.center-letter {
  fill: #1a1a1a;
}

/* ===== Input Area ===== */
#input-area {
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 2px;
  text-transform: uppercase;
  padding: 4px 16px;
}

#cursor {
  animation: blink 1s step-end infinite;
  color: var(--text-secondary);
  font-weight: 300;
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ===== Controls ===== */
#controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 8px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.ctrl-btn {
  padding: 12px 28px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 24px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  font-family: var(--font);
}

.ctrl-btn:active {
  background: var(--bg-secondary);
  transform: scale(0.95);
}

.icon-ctrl {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===== Overlays ===== */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.overlay:not(.hidden) {
  opacity: 1;
  pointer-events: auto;
}

.overlay-content {
  background: var(--card-bg);
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  border-radius: 16px 16px 0 0;
  overflow-y: auto;
  padding: 20px;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}

.overlay:not(.hidden) .overlay-content {
  transform: translateY(0);
}

.overlay-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.overlay-header h2 {
  font-size: 18px;
  font-weight: 700;
}

/* ===== Hints Table ===== */
#hints-content {
  overflow-x: hidden;
}

.hints-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  margin-bottom: 20px;
  table-layout: fixed;
}

.hints-table th,
.hints-table td {
  padding: 6px 2px;
  text-align: center;
  border: 1px solid var(--border);
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--bg-secondary);
}

.hints-table th {
  background: var(--bg-secondary);
  font-weight: 600;
}

.hints-table td.solved {
  background: var(--success-light);
  color: var(--success);
  font-weight: 600;
}

.hints-table td.partial {
  background: var(--bg-secondary);
  color: var(--text);
}

.hints-table td.clickable {
  cursor: pointer;
  transition: background 0.15s;
}

.hints-table td.clickable:hover {
  background: var(--border);
}

.hints-table td.empty {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  opacity: 0.3;
}

.hints-table .row-header {
  font-weight: 600;
  background: var(--bg-secondary);
  text-align: left;
  padding-left: 6px;
}

.hints-table .col-header {
  font-weight: 600;
}

.hints-table .total-cell {
  font-weight: 700;
  background: var(--bg-secondary);
}

.hints-table .solved-total {
  background: var(--success-light);
  color: var(--success);
  font-weight: 700;
}



/* Two-letter pairs */
.two-letter-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 10px;
}

.two-letter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
  gap: 6px;
}

.two-letter-item {
  padding: 6px;
  background: var(--bg-secondary);
  border-radius: 6px;
  font-size: 13px;
  text-align: center;
  font-family: monospace;
  white-space: nowrap;
}

.two-letter-item.solved {
  background: var(--success-light);
  color: var(--success);
  font-weight: 600;
}

.two-letter-item.partial {
  color: var(--text);
}

.two-letter-item.clickable {
  cursor: pointer;
  transition: background 0.15s;
}

.two-letter-item.clickable:active {
  background: var(--border);
}

/* ===== Pangram Hints ===== */
.pangram-hints-section {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.pangram-hints-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
}

.pangram-hints-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.pangram-hint-level {
  font-size: 14px;
  color: var(--text);
}

.pangram-hint-level .hint-label {
  font-weight: 600;
  color: var(--text-secondary);
  margin-right: 8px;
}

.pangram-hint-level .hint-letter,
.pangram-hint-level .hint-length,
.pangram-hint-level .hint-prefix {
  display: inline-block;
  padding: 2px 8px;
  background: var(--bg-secondary);
  border-radius: 4px;
  font-family: monospace;
  font-weight: 600;
}

.pangram-hint-level .hint-letter.found,
.pangram-hint-level .hint-length.found,
.pangram-hint-level .hint-prefix.found {
  background: var(--success-light);
  color: var(--success);
}

.pangram-hint-level .hint-word {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 6px;
  font-family: monospace;
  font-weight: 600;
  letter-spacing: 1px;
}

.pangram-hint-level .hint-word.found {
  background: var(--success-light);
  color: var(--success);
}

.pangram-hint-level .hint-word.partial {
  background: var(--bg-secondary);
  color: var(--text);
}

.pangram-hint-level .hint-word.revealed {
  background: var(--pangram-bg);
  color: #fff;
}

.pangram-hint-btn {
  display: block;
  margin: 0 auto;
  padding: 8px 16px;
}

/* ===== Cell Hints (Approach A - Inline) ===== */
.cell-hints-inline {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.cell-hints-inline h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
}

.cell-hints-inline .cell-hint-word {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.cell-hints-inline .cell-hint-word:last-child {
  border-bottom: none;
}

.cell-hints-inline .hint-pattern {
  font-family: monospace;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 2px;
}

.cell-hints-inline .hint-pattern .revealed {
  color: var(--text);
}

.cell-hints-inline .hint-pattern .hidden {
  color: var(--text-secondary);
}

.cell-hints-inline .hint-pattern .found {
  color: var(--success);
}

.cell-hints-inline .reveal-btn {
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-secondary);
  border: 1px solid var(--hex-stroke);
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}

.cell-hints-inline .reveal-btn:active {
  background: var(--border);
}

.cell-hints-inline .reveal-btn.add-to-solved {
  background: var(--success-light);
  color: var(--success);
  border-color: var(--success);
  cursor: pointer;
}

.cell-hints-inline .reveal-btn.add-to-solved:hover {
  background: var(--success);
  color: var(--bg);
}

/* ===== Cell Hints (Approach B - Overlay) ===== */
#cell-hints-overlay .cell-hint-word {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

#cell-hints-overlay .cell-hint-word:last-child {
  border-bottom: none;
}

#cell-hints-overlay .hint-pattern {
  font-family: monospace;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 3px;
}

#cell-hints-overlay .hint-pattern .revealed {
  color: var(--text);
}

#cell-hints-overlay .hint-pattern .hidden {
  color: var(--text-secondary);
}

#cell-hints-overlay .hint-pattern .found {
  color: var(--success);
}

#cell-hints-overlay .reveal-btn {
  padding: 8px 16px;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-secondary);
  border: 1px solid var(--hex-stroke);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

#cell-hints-overlay .reveal-btn:active {
  background: var(--border);
}

#cell-hints-overlay .reveal-btn.add-to-solved {
  background: var(--success-light);
  color: var(--success);
  border-color: var(--success);
  cursor: pointer;
}

#cell-hints-overlay .reveal-btn.add-to-solved:hover {
  background: var(--success);
  color: var(--bg);
}

.cell-hint-summary {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.cell-hint-word.found {
  opacity: 0.7;
}

/* ===== Settings ===== */
.text-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  padding: 8px 0;
  font-family: var(--font);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.text-btn:active {
  opacity: 0.7;
}

.primary-btn {
  background: var(--accent);
  color: #1a1a1a;
  border: none;
  padding: 14px 32px;
  border-radius: 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
  transition: background 0.15s, transform 0.1s;
  width: 100%;
}

.primary-btn:active {
  background: var(--accent-dark);
  transform: scale(0.98);
}

/* ===== History List ===== */
#history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px;
  background: var(--bg-secondary);
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s;
}

.history-item:active {
  background: var(--border);
}

.history-item-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.history-item-letters {
  font-weight: 600;
  font-size: 16px;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.history-item-letters .center-letter {
  color: var(--accent);
}

.history-item-stats {
  font-size: 13px;
  color: var(--text-secondary);
}

.history-item-status {
  font-size: 11px;
  font-weight: 500;
  margin-top: 2px;
}

.history-item-status.completed {
  color: var(--success);
}

.history-item-status.revealed {
  color: var(--text-secondary);
}

.history-item-status.in-progress {
  color: var(--accent);
}

.history-item-rank {
  font-size: 14px;
  font-weight: 500;
  color: var(--accent);
}

/* ===== Solution ===== */
#solution-content {
  margin-bottom: 20px;
}

.solution-word-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.solution-word {
  padding: 6px 12px;
  background: var(--bg-secondary);
  border-radius: 12px;
  font-size: var(--found-words-font-size, 15px);
  text-transform: capitalize;
  text-decoration: none;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  display: inline-block;
}

.solution-word:active {
  transform: scale(0.95);
  opacity: 0.8;
}

.solution-word.found {
  background: var(--success-light);
  color: var(--success);
}



.solution-word.pangram {
  background: var(--pangram-bg);
  font-weight: 600;
  color: #fff;
}

.solution-word.pangram.found {
  background: var(--success);
  color: white;
}

.solution-word.pangram.missed {
  background: var(--pangram-bg);
  color: #fff;
  opacity: 0.7;
}

.solution-word.missed {
  background: var(--bg-secondary);
  color: var(--text-secondary);
}

.solution-summary {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 16px;
  font-size: 14px;
  font-weight: 500;
}

.found-summary {
  color: var(--success);
}

.missed-summary {
  color: var(--text-secondary);
}

.solution-section {
  margin-bottom: 16px;
}

.solution-section h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.solution-stats {
  display: flex;
  gap: 20px;
  margin-bottom: 16px;
  padding: 12px;
  background: var(--bg-secondary);
  border-radius: 12px;
}

.solution-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.solution-stat-value {
  font-size: 24px;
  font-weight: 700;
}

.solution-stat-label {
  font-size: 12px;
  color: var(--text-secondary);
}

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

/* ===== Complete Modal ===== */
#complete-message {
  text-align: center;
  padding: 20px 0;
}

#complete-message h3 {
  font-size: 20px;
  margin-bottom: 8px;
}

#complete-message p {
  color: var(--text-secondary);
}

/* ===== Animations ===== */
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.pop {
  animation: pop 0.2s ease;
}

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

.shake {
  animation: shake 0.5s ease;
}

.center-letter-highlight {
  color: var(--accent);
  font-weight: 700;
}

.solution-letters-bar {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 16px;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.solution-letters-bar .solution-letter {
  padding: 4px 8px;
  background: var(--bg-secondary);
  border-radius: 8px;
}

.solution-letters-bar .solution-letter.center {
  background: var(--accent);
  color: #1a1a1a;
}

.font-size-btn.active {
  background: var(--accent);
  color: #1a1a1a;
  border-color: var(--accent);
}

/* ===== Settings ===== */
.settings-section {
  margin-bottom: 24px;
}

.settings-section h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-child {
  border-bottom: none;
}

.settings-label {
  font-size: 15px;
  color: var(--text);
}

.settings-options {
  display: flex;
  gap: 8px;
}

.settings-btn {
  padding: 8px 16px;
  font-size: 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

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

.settings-btn.active {
  background: var(--accent);
  color: #1a1a1a;
  border-color: var(--accent);
  font-weight: 600;
}

/* ===== Give Up Area ===== */
#give-up-area {
  text-align: center;
  padding: 0 16px 8px;
}

.give-up-btn {
  color: var(--text-secondary);
  font-size: 13px;
  opacity: 0.7;
  transition: opacity 0.15s;
}

.give-up-btn:hover,
.give-up-btn:active {
  opacity: 1;
}

/* ===== Responsive ===== */
@media (max-height: 600px) {
  #hive svg {
    max-width: 220px;
  }
  
  #score-area {
    padding: 0.5em 1em;
  }
  
  #controls {
    padding: 10px 16px;
  }
}

@media (max-height: 500px) {
  #hive svg {
    max-width: 180px;
  }
}

@media (min-width: 421px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}