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

:root {
  --bg: #f0ede6;
  --surface: #faf8f4;
  --border: #ddd9cf;
  --text: #1a1a1a;
  --muted: #bbb;
  --accent: #2a2a2a;
  --sel: #e8f0ff;
  --sel-border: #7a9ee8;
  --danger: #c0392b;
  --saved-bg: #f0f7f0;
  --saved-border: #90c890;
  --panel-bg: #e8e4dc;
  --panel-border: #d8d4cc;
  --empty-bg: #e8e4da;
  --empty-border: #d4d0c6;
  --tile-shadow: 0 2px 6px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.08);
  --tile-shadow-lift: 0 4px 14px rgba(0,0,0,0.18), 0 2px 4px rgba(0,0,0,0.1);
  --overlay-bg: rgba(30,28,24,0.45);
}

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

body {
  font-family: 'Georgia', serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* --- Header --- */
header {
  padding: 0.8rem 1.1rem 0.65rem;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  background: var(--panel-bg);
  position: relative;
  z-index: 10;
}
.header-left { display: flex; align-items: baseline; gap: 0.7rem; }
h1 { font-size: 1.25rem; font-weight: normal; letter-spacing: -0.02em; }
.subtitle { color: var(--muted); font-size: 0.7rem; font-style: italic; }

#reroll-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem 0.7rem;
  font-size: 0.75rem;
  font-family: inherit;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.1s, border-color 0.1s;
  background: var(--surface);
}
#reroll-btn:active { background: var(--bg); border-color: #bbb; }

/* --- Grid --- */
#grid-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  overflow: hidden;
}

#board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.55rem;
  width: 100%;
  max-width: 380px;
}

/* --- Tiles --- */
.word-card {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: grab;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  box-shadow: var(--tile-shadow);
  transition: transform 0.1s, box-shadow 0.1s, background 0.1s, border-color 0.1s;
  overflow: hidden;
  padding: 0.4rem;
}
.word-card.empty {
  background: var(--empty-bg);
  border: 1.5px dashed var(--empty-border);
  box-shadow: none;
  cursor: default;
}
.word-card.sliding {
  transform: scale(0.92);
  box-shadow: var(--tile-shadow-lift);
  background: #fff;
  border-color: var(--sel-border);
  z-index: 10;
}

.word-text {
  font-size: clamp(0.72rem, 4vw, 1rem);
  font-weight: normal;
  letter-spacing: 0.01em;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
  pointer-events: none;
  padding: 0 0.2rem;
}

.card-actions {
  position: absolute;
  top: 3px; right: 3px;
  display: flex; gap: 1px;
}
.card-actions button {
  background: none; border: none; cursor: pointer;
  font-size: 0.58rem; color: var(--muted);
  padding: 2px 2px; line-height: 1;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.card-actions .save-btn:active { color: var(--saved-border); }
.card-actions .discard-btn:active { color: var(--danger); }

.slide-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  color: var(--sel-border);
  opacity: 0;
  pointer-events: none;
}
.word-card.sliding .slide-hint { opacity: 0.5; }

/* --- Smash overlay (drops from top) --- */
#smash-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 200;
  pointer-events: none;
}

#smash-overlay.visible {
  pointer-events: all;
}

/* dim backdrop - fades in */
#smash-backdrop {
  position: absolute;
  inset: 0;
  background: var(--overlay-bg);
  opacity: 0;
  transition: opacity 0.22s ease;
}
#smash-overlay.visible #smash-backdrop {
  opacity: 1;
}

/* panel that slides down from top */
#smash-panel {
  position: absolute;
  top: 0; left: 0; right: 0;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
  padding: 0.9rem 1rem 0.85rem;
  transform: translateY(-110%);
  transition: transform 0.28s cubic-bezier(0.34, 1.2, 0.64, 1);
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
}
#smash-overlay.visible #smash-panel {
  transform: translateY(0);
}

#smash-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.6rem;
}
#smash-words { font-size: 0.78rem; color: var(--muted); font-style: italic; }
#cancel-smash {
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 0.75rem; font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}
#cancel-smash:active { color: var(--danger); }

#overlap-strip {
  display: flex; gap: 0.35rem;
  overflow-x: auto; scrollbar-width: none;
  padding-bottom: 0.55rem; margin-bottom: 0.6rem;
  scroll-snap-type: x mandatory;
}
#overlap-strip::-webkit-scrollbar { display: none; }

.overlap-chip {
  flex-shrink: 0;
  scroll-snap-align: start;
  padding: 0.38rem 0.75rem;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 20px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: border-color 0.1s, background 0.1s;
}
.overlap-chip.selected { border-color: var(--sel-border); background: var(--sel); }
.overlap-chip .chip-shared {
  color: var(--sel-border);
  text-decoration: underline;
  text-decoration-color: var(--sel-border);
}

#confirm-smash {
  width: 100%; padding: 0.5rem;
  background: var(--accent); color: #fff;
  border: none; border-radius: 8px;
  font-size: 0.88rem; font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
#confirm-smash:active { background: #444; }

/* --- Bottom panel (saved only) --- */
#bottom-panel {
  flex-shrink: 0;
  background: var(--panel-bg);
  border-top: 1px solid var(--panel-border);
}

#saved-area { padding: 0 1rem; }

#saved-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
#saved-toggle h2 {
  font-size: 0.62rem;
  font-weight: normal;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}
#saved-chevron {
  font-size: 0.6rem; color: var(--muted);
  transition: transform 0.2s;
}
#saved-chevron.open { transform: rotate(180deg); }

#saved-list {
  display: none;
  flex-wrap: wrap;
  gap: 0.38rem;
  padding-bottom: 0.8rem;
  max-height: 30vh;
  overflow-y: auto;
}
#saved-list.open { display: flex; }
.empty-msg { color: var(--muted); font-style: italic; font-size: 0.78rem; }

.saved-item {
  display: inline-flex; align-items: center; gap: 0.3rem;
  padding: 0.25rem 0.6rem;
  background: var(--saved-bg); border: 1px solid var(--saved-border);
  border-radius: 20px; font-size: 0.8rem;
}
.saved-item .remove-btn {
  background: none; border: none; cursor: pointer;
  color: var(--saved-border); font-size: 0.68rem; line-height: 1;
  -webkit-tap-highlight-color: transparent;
}
.saved-item .remove-btn:active { color: var(--danger); }

/* --- Place bar --- */
#place-bar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 1.1rem;
  background: #fff8e8;
  border-bottom: 1px solid #e8d8a0;
  font-size: 0.78rem;
  color: #7a6020;
  flex-shrink: 0;
  position: relative;
  z-index: 5;
}
#place-bar.visible { display: flex; }
#place-bar strong { font-weight: normal; font-style: italic; }
#place-bar-cancel {
  background: none; border: none; cursor: pointer;
  color: #b09040; font-size: 0.72rem; font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}

/* place-target tiles pulse gently */
.word-card.place-target {
  cursor: pointer;
  animation: pulse-target 1.4s ease-in-out infinite;
}
@keyframes pulse-target {
  0%, 100% { box-shadow: var(--tile-shadow); }
  50% { box-shadow: 0 0 0 2px #e8c860, var(--tile-shadow); }
}
.word-card.empty.place-target {
  border-color: #e8c860;
  background: #fffbe8;
  animation: pulse-empty 1.4s ease-in-out infinite;
}
@keyframes pulse-empty {
  0%, 100% { border-color: #e8c860; }
  50% { border-color: #c8a820; }
}

/* smash hint text */
.smash-hint {
  font-size: 0.7rem;
  color: var(--muted);
  font-style: italic;
  margin-bottom: 0.5rem;
}

/* confirm button disabled state */
#confirm-smash:disabled {
  cursor: default;
}

/* place button on saved items */
.saved-item .place-btn {
  background: none; border: none; cursor: pointer;
  color: #b09040; font-size: 0.72rem; line-height: 1;
  padding: 0 2px;
  -webkit-tap-highlight-color: transparent;
}
.saved-item .place-btn:active { color: #7a6020; }
