/* =============================================
   ELG Puzzle Game — v2.4
   ============================================= */

:root {
  --elg-font: "DM Sans", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --elg-font-display: "Fraunces", "Georgia", serif;
  --elg-bg: #FAFAF8;
  --elg-board-bg: #E2E0DA;
  --elg-cell-bg: #D0CEC7;
  --elg-cell-radius: 4px;
  --elg-ink: #1A1A1A;
  --elg-muted: #7A7A76;
  --elg-border: #CDCDC6;
  --elg-piece-radius: 4px;
  --elg-piece-shadow: 0 2px 6px rgba(0,0,0,0.12);
  --elg-piece-shadow-drag: 0 14px 36px rgba(0,0,0,0.24);
  --elg-transition: 120ms ease;
  --elg-red: #D4626E;
  --elg-yellow: #D4A843;
  --elg-blue: #4A94B0;
  --elg-sage: #5E9E6E;
  --elg-locked: #3A3A3C;
  --elg-piece-border: rgba(255,255,255,0.85);
  --elg-board-size: clamp(340px, 42vw, 520px);
  --elg-board-pad: 10px;
  --elg-tray-width: clamp(300px, 36vw, 460px);
}

.elg-puzzle-shell {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 20px;
  font-family: var(--elg-font);
  color: var(--elg-ink);
}

/* --- Top bar --- */
.elg-puzzle-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--elg-border);
}
.elg-topbar-left { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.elg-topbar-right { display: flex; align-items: center; gap: 10px; }

.elg-title { font-family: var(--elg-font-display); font-weight: 600; font-size: 22px; letter-spacing: -0.3px; }
.elg-daily-date { font-size: 13px; font-weight: 500; color: var(--elg-muted); }

/* Difficulty */
.elg-difficulty { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; letter-spacing: 0.3px; text-transform: uppercase; color: var(--elg-muted); }
.elg-difficulty-dots { display: flex; gap: 3px; }
.elg-difficulty-dot { width: 8px; height: 8px; border-radius: 2px; background: var(--elg-border); transition: background 200ms; }
.elg-difficulty-dot.is-filled { background: var(--elg-ink); }

/* Streak */
.elg-streak { font-size: 14px; font-weight: 700; color: var(--elg-ink); white-space: nowrap; }
.elg-streak[hidden] { display: none; }

/* Timer */
.elg-timer { font-variant-numeric: tabular-nums; font-size: 15px; font-weight: 600; color: var(--elg-muted); min-width: 48px; text-align: center; letter-spacing: 0.5px; }
.elg-timer.is-stopped { color: var(--elg-ink); }

/* Buttons */
.elg-actions { display: flex; gap: 6px; align-items: center; }

.elg-actions button,
button.elg-btn {
  appearance: none;
  border: 1px solid var(--elg-border);
  cursor: pointer;
  font-family: var(--elg-font);
  font-weight: 600;
  font-size: 13px;
  padding: 8px 12px;
  border-radius: 6px;
  background: #fff;
  color: var(--elg-ink);
  transition: background var(--elg-transition), border-color var(--elg-transition);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  line-height: 1;
}

.elg-actions button:hover, button.elg-btn:hover { background: #F3F3F0; border-color: #B0B0A8; }
.elg-actions button:active, button.elg-btn:active { background: #EDEDEA; transform: translateY(1px); }
.elg-actions button:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }
.elg-actions button svg { width: 14px; height: 14px; flex-shrink: 0; }

/* --- Layout --- */
.elg-puzzle-main { display: flex; gap: 32px; justify-content: center; align-items: flex-start; }

/* --- Board --- */
.elg-board-wrap { flex: 0 0 auto; }
.elg-board {
  position: relative;
  width: var(--elg-board-size); height: var(--elg-board-size);
  border-radius: 10px;
  background: var(--elg-board-bg);
  box-shadow: 0 1px 3px rgba(0,0,0,0.06), inset 0 1px 0 rgba(255,255,255,0.5);
  overflow: hidden;
  user-select: none;
  touch-action: none;
}
.elg-board-grid { position: absolute; inset: 0; display: grid; gap: 3px; padding: var(--elg-board-pad); box-sizing: border-box; }
.elg-cell { background: var(--elg-cell-bg); border-radius: var(--elg-cell-radius); }
.elg-drop-ghost { position: absolute; pointer-events: none; z-index: 1; transition: left 60ms, top 60ms; border-radius: var(--elg-piece-radius); }

/* Status */
.elg-status { margin-top: 10px; font-size: 13px; font-weight: 500; color: var(--elg-muted); min-height: 22px; line-height: 22px; }

/* --- Win banner --- */
.elg-win-banner {
  display: none; flex-direction: column; align-items: center; gap: 14px;
  margin-top: 20px; padding: 24px 28px; border-radius: 12px;
  background: #F5F4F0; border: 1px solid var(--elg-border);
  text-align: center; animation: elg-fadeIn 400ms ease;
}
.elg-win-banner.is-visible { display: flex; }
.elg-win-title { font-family: var(--elg-font-display); font-size: 22px; font-weight: 600; }
.elg-win-stats { display: flex; gap: 28px; font-size: 12px; color: var(--elg-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.elg-win-stat-value { font-family: var(--elg-font); font-weight: 700; color: var(--elg-ink); font-size: 20px; display: block; margin-bottom: 3px; font-variant-numeric: tabular-nums; letter-spacing: 0; text-transform: none; }

/* Share group — share + copy side by side */
.elg-share-group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: center; }

.elg-share-btn {
  appearance: none; border: none; cursor: pointer;
  font-family: var(--elg-font); font-weight: 700; font-size: 14px;
  padding: 11px 24px; border-radius: 999px;
  background: var(--elg-ink); color: #fff;
  display: inline-flex; align-items: center; gap: 6px;
  transition: opacity 150ms;
}
.elg-share-btn:hover { opacity: 0.85; }
.elg-share-btn:active { transform: translateY(1px); }
.elg-share-btn svg { width: 16px; height: 16px; }

@keyframes elg-fadeIn { from { opacity:0; transform:translateY(8px); } to { opacity:1; transform:translateY(0); } }

/* --- Tray --- */
.elg-tray-wrap { flex: 0 0 auto; width: var(--elg-tray-width); }
.elg-tray-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.elg-tray-title { font-weight: 600; font-size: 12px; letter-spacing: 0.8px; text-transform: uppercase; color: var(--elg-muted); }

.elg-tray-toggle {
  display: none; /* hidden on desktop, shown on mobile */
  appearance: none; border: 1px solid var(--elg-border);
  background: #fff; border-radius: 6px;
  width: 32px; height: 32px;
  cursor: pointer; align-items: center; justify-content: center;
  padding: 0;
}
.elg-tray-chevron { width: 16px; height: 16px; color: var(--elg-ink); transition: transform 200ms ease; }
.elg-tray-wrap.is-expanded .elg-tray-chevron { transform: rotate(180deg); }
.elg-tray { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; box-sizing: border-box; }
.elg-tray-slot { position: relative; aspect-ratio: 4/3; border-radius: 8px; background: #EEEDE8; overflow: hidden; border: 1px solid rgba(0,0,0,0.04); }

.elg-tray-fitwrap { --trayScale:1; position: absolute; inset: 0; transform-origin: 50% 50%; will-change: transform; transform: scale(var(--trayScale)); pointer-events: none; }
.elg-tray-fitwrap > .elg-piece { pointer-events: auto; }

/* --- Pieces --- */
.elg-piece {
  position: absolute; border-radius: var(--elg-piece-radius); box-sizing: border-box;
  box-shadow: var(--elg-piece-shadow); cursor: grab; touch-action: none; user-select: none;
  transition: box-shadow var(--elg-transition);
}
.elg-piece.is-dragging { cursor: grabbing; box-shadow: var(--elg-piece-shadow-drag); z-index: 999999; }

.elg-piece[data-color="red"]    { background: var(--elg-red);    border: 2px solid var(--elg-piece-border); }
.elg-piece[data-color="yellow"] { background: var(--elg-yellow); border: 2px solid var(--elg-piece-border); }
.elg-piece[data-color="blue"]   { background: var(--elg-blue);   border: 2px solid var(--elg-piece-border); }
.elg-piece[data-color="white"]  { background: var(--elg-sage);   border: 2px solid var(--elg-piece-border); }
.elg-piece[data-color="locked"] { background: var(--elg-locked); border: 2px solid var(--elg-piece-border); cursor: default; box-shadow: none; }
.elg-board .elg-piece { transform: none; }

/* Piece hint flash */
.elg-piece.is-hinted {
  animation: elg-hintFlash 600ms ease;
}
@keyframes elg-hintFlash {
  0%, 100% { box-shadow: var(--elg-piece-shadow); }
  50% { box-shadow: 0 0 0 4px rgba(74,148,176,0.5), var(--elg-piece-shadow); }
}

/* Labels */
.elg-piece-label {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  font-size: 14px; font-weight: 800; letter-spacing: 0.3px;
  color: rgba(255,255,255,0.85); text-shadow: 0 1px 3px rgba(0,0,0,0.25);
  pointer-events: none; white-space: nowrap;
}
.elg-piece[data-color="locked"] .elg-piece-label { color: rgba(255,255,255,0.35); }
.elg-board .elg-piece-label, .elg-piece.is-dragging .elg-piece-label { display: none; }
.elg-tray-fitwrap .elg-piece-label { transform: translate(-50%,-50%) scale(calc(1 / var(--trayScale,1))); }

/* Level nav */
.elg-level-nav { display: flex; align-items: center; justify-content: center; gap: 12px; margin-bottom: 20px; }
.elg-level-nav button {
  appearance: none; border: 1px solid var(--elg-border); cursor: pointer;
  font-family: var(--elg-font); font-weight: 600; font-size: 13px;
  padding: 7px 14px; border-radius: 6px; background: #fff; color: var(--elg-ink);
  transition: background var(--elg-transition); display: inline-flex; align-items: center; gap: 4px; line-height: 1;
}
.elg-level-nav button:hover { background: #F3F3F0; }
.elg-level-nav button:active { transform: translateY(1px); }
.elg-level-nav button:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.elg-level-nav button svg { width: 14px; height: 14px; }
.elg-level-current { font-weight: 700; font-size: 14px; min-width: 80px; text-align: center; font-variant-numeric: tabular-nums; }

/* Login bar */
.elg-login-bar { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-bottom: 4px; font-size: 13px; color: var(--elg-muted); }
.elg-login-bar a { color: var(--elg-ink); text-decoration: none; font-weight: 600; }
.elg-login-bar a:hover { text-decoration: underline; }
.elg-user-avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--elg-border); display: inline-flex; align-items: center; justify-content: center; overflow: hidden; }
.elg-user-avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ==========================
   Modals (HTP, login gate, daily-done)
   ========================== */
.elg-modal-overlay {
  position: fixed; inset: 0; z-index: 2000000;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; animation: elg-fadeIn 200ms ease;
}
.elg-modal-overlay[hidden] { display: none !important; }

.elg-modal {
  background: #fff; border-radius: 16px; padding: 32px 36px 28px;
  max-width: 480px; width: 100%; position: relative;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  animation: elg-modalIn 300ms ease;
}
@keyframes elg-modalIn { from { opacity:0; transform:scale(0.95) translateY(10px); } to { opacity:1; transform:scale(1) translateY(0); } }

.elg-modal-close {
  position: absolute; top: 14px; right: 16px;
  appearance: none; border: none; background: none;
  font-size: 24px; cursor: pointer; color: var(--elg-muted); line-height: 1; padding: 4px;
}
.elg-modal-close:hover { color: var(--elg-ink); }

.elg-modal-title { font-family: var(--elg-font-display); font-size: 24px; font-weight: 600; margin: 0 0 24px 0; text-align: center; }

/* HTP steps */
.elg-htp-steps { display: flex; flex-direction: column; gap: 20px; margin-bottom: 8px; }
.elg-htp-step { display: flex; align-items: flex-start; gap: 14px; }
.elg-htp-icon { flex: 0 0 auto; width: 48px; height: 48px; display: flex; align-items: center; justify-content: center; background: #F5F4F0; border-radius: 10px; color: var(--elg-ink); }
.elg-htp-step h3 { margin: 0 0 4px; font-size: 15px; font-weight: 700; }
.elg-htp-step p { margin: 0; font-size: 13px; color: var(--elg-muted); line-height: 1.5; }
.elg-htp-step kbd { display: inline-block; padding: 1px 6px; border: 1px solid var(--elg-border); border-radius: 4px; background: #F5F4F0; font-family: var(--elg-font); font-size: 12px; font-weight: 700; }

/* Toast */
.elg-toast {
  position: fixed; top: 20px; left: 50%;
  transform: translateX(-50%) translateY(-60px);
  background: var(--elg-ink); color: #fff;
  font-family: var(--elg-font); font-size: 13px; font-weight: 600;
  padding: 10px 20px; border-radius: 8px;
  z-index: 100000; pointer-events: none; opacity: 0;
  transition: transform 300ms ease, opacity 300ms ease;
}
.elg-toast.is-visible { transform: translateX(-50%) translateY(0); opacity: 1; }

/* --- Responsive: Tablet --- */
@media (max-width: 960px) {
  :root { --elg-board-size: min(88vw, 520px); --elg-tray-width: min(88vw, 520px); }
  .elg-puzzle-main { flex-direction: column; align-items: center; gap: 24px; }
  .elg-win-banner { max-width: min(88vw, 520px); margin-left: auto; margin-right: auto; }
}

/* --- Responsive: Mobile --- */
@media (max-width: 600px) {
  :root {
    --elg-board-size: calc(100vw - 24px);
    --elg-tray-width: 100%;
    --elg-board-pad: 6px;
  }

  .elg-puzzle-shell { padding: 0 8px; }

  /* Compact top bar: two rows */
  .elg-puzzle-topbar {
    flex-wrap: wrap;
    margin-bottom: 8px;
    padding-bottom: 8px;
    gap: 6px;
  }

  .elg-topbar-left {
    width: 100%;
    justify-content: space-between;
    gap: 8px;
  }

  .elg-topbar-right {
    width: 100%;
    justify-content: space-between;
    gap: 4px;
  }

  .elg-title { font-size: 17px; }
  .elg-daily-date { font-size: 11px; }
  .elg-difficulty { font-size: 10px; }
  .elg-difficulty-dot { width: 6px; height: 6px; }
  .elg-timer { font-size: 14px; min-width: 40px; }
  .elg-streak { font-size: 13px; }

  /* Compact action buttons — icon-only where possible */
  .elg-actions { gap: 4px; flex: 1; justify-content: flex-end; }
  .elg-actions button {
    padding: 6px 8px;
    font-size: 11px;
    border-radius: 5px;
  }
  .elg-actions button svg { width: 16px; height: 16px; }

  /* Level nav */
  .elg-level-nav { margin-bottom: 10px; gap: 8px; }
  .elg-level-nav button { padding: 6px 10px; font-size: 12px; }
  .elg-level-current { font-size: 13px; min-width: 60px; }

  /* Main layout */
  .elg-puzzle-main {
    flex-direction: column;
    align-items: center;
    gap: 0;
  }

  /* Board: near full width */
  .elg-board { border-radius: 8px; }
  .elg-board-wrap { padding-bottom: 0; width: 100%; }

  /* Win banner: inline, above tray */
  .elg-win-banner {
    margin-top: 12px;
    padding: 16px 16px;
    border-radius: 10px;
    gap: 10px;
    margin-bottom: 12px;
  }
  .elg-win-title { font-size: 18px; }
  .elg-win-stat-value { font-size: 18px; }
  .elg-win-stats { gap: 20px; }
  .elg-share-group { gap: 6px; }
  .elg-share-btn { padding: 9px 18px; font-size: 13px; }
  button.elg-btn { font-size: 12px; padding: 7px 12px; }

  /* Fixed bottom tray */
  .elg-tray-wrap {
    width: 100%;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    background: var(--elg-bg);
    border-top: 1px solid var(--elg-border);
    padding: 0 8px calc(8px + env(safe-area-inset-bottom));
    z-index: 100;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.08);
    transition: max-height 300ms ease;
    max-height: 100px;
    overflow: hidden;
  }

  .elg-tray-header {
    display: flex;
    padding: 8px 4px 4px;
    margin-bottom: 4px;
  }
  .elg-tray-title { display: block; font-size: 11px; }
  .elg-tray-toggle { display: flex; }

  /* Collapsed: horizontal scroll strip */
  .elg-tray {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 6px;
    padding-bottom: 2px;
    scrollbar-width: none;
  }
  .elg-tray::-webkit-scrollbar { display: none; }
  .elg-tray-slot {
    flex: 0 0 80px;
    height: 64px;
    aspect-ratio: auto;
    border-radius: 6px;
  }

  /* Expanded: grid showing all pieces */
  .elg-tray-wrap.is-expanded {
    max-height: 60vh;
    overflow-y: auto;
    border-radius: 16px 16px 0 0;
  }
  .elg-tray-wrap.is-expanded .elg-tray {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    overflow: visible;
    padding-bottom: 8px;
  }
  .elg-tray-wrap.is-expanded .elg-tray-slot {
    flex: none;
    width: auto;
    height: 72px;
  }

  /* Space for fixed tray at bottom */
  .elg-board-wrap { margin-bottom: 100px; }
  .elg-tray-wrap.is-expanded ~ .elg-board-wrap,
  .elg-puzzle-main:has(~ .elg-tray-wrap.is-expanded) { margin-bottom: 200px; }

  /* Status text */
  .elg-status { font-size: 12px; margin-top: 6px; }

  /* Modals */
  .elg-modal { padding: 20px 16px 16px; max-width: calc(100vw - 32px); }
  .elg-modal-title { font-size: 18px; margin-bottom: 16px; }
  .elg-htp-step h3 { font-size: 14px; }
  .elg-htp-step p { font-size: 12px; }
  .elg-htp-icon { width: 40px; height: 40px; }
  .elg-htp-icon svg { width: 32px; height: 32px; }
  .elg-htp-steps { gap: 14px; }

  /* Login bar */
  .elg-login-bar { font-size: 12px; gap: 8px; }
}
