@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Bebas+Neue&display=swap');

:root {
  --bg: #050505;
  --panel: #0d0d0d;
  --border: #262626;
  --accent: #ffffff;
  --text: #e0e0e0;
  --muted: #666666;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Space Mono', monospace;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(rgba(255,255,255,0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.02) 1px, transparent 1px);
  background-size: 40px 40px;
  pointer-events: none;
  z-index: 0;
}

::selection { background: #444; color: #fff; }

/* ============================================================
   HEADER
   ============================================================ */
header {
  flex: 0 0 auto;
  position: relative;
  z-index: 10;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(5,5,5,0.95);
  backdrop-filter: blur(10px);
}
header h1 {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.8rem;
  letter-spacing: 4px;
  color: var(--accent);
}
.github-link {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.7rem;
  letter-spacing: 2px;
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 4px;
  transition: all 0.2s;
}
.github-link:hover { color: var(--accent); border-color: var(--accent); }

/* ============================================================
   LAYOUT
   ============================================================ */
.layout {
  flex: 1;
  min-height: 0;
  display: flex;
  position: relative;
  z-index: 1;
}

/* ============================================================
   LEFT PANEL
   ============================================================ */
.left-panel {
  width: 500px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  overflow-y: auto;
}

.section {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.solver-section { flex: 1; min-height: 0; }

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.section-label {
  font-size: 0.65rem;
  letter-spacing: 3px;
  color: var(--accent);
  text-transform: uppercase;
}
.header-actions { display: flex; gap: 6px; align-items: center; }

/* ============================================================
   INPUTS & BUTTONS
   ============================================================ */
.scramble-row { display: flex; gap: 8px; }

input[type=text] {
  flex: 1;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  font-family: 'Space Mono', monospace;
  font-size: 0.75rem;
  padding: 8px 12px;
  outline: none;
  transition: border-color 0.2s;
  min-width: 0;
}
input[type=text]:focus { border-color: var(--accent); }

.btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  font-family: 'Space Mono', monospace;
  font-size: 0.75rem;
  padding: 8px 14px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  letter-spacing: 1px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:active { opacity: 0.7; transform: scale(0.97); }
.btn.mini { padding: 4px 10px; font-size: 0.6rem; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #000; font-weight: 700; }
.btn.primary:hover { background: #ccc; border-color: #ccc; }
.btn.danger { border-color: var(--border); color: #aaa; }
.btn.danger:hover { background: #333; color: #fff; border-color: #333; }

/* Desktop scan button visible on desktop, hidden on mobile (mobile uses tab bar) */
.scan-desktop-btn { display: inline-flex; }

/* ============================================================
   COLOR PALETTE
   ============================================================ */
.color-palette {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  flex-wrap: wrap;
  justify-content: center;
}
.color-swatch {
  width: 32px; height: 32px;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.15s;
  position: relative;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.color-swatch.selected {
  border-color: #fff;
  box-shadow: 0 0 0 2px rgba(255,255,255,0.5);
  transform: scale(1.15);
  z-index: 2;
}
.color-swatch span {
  position: absolute;
  bottom: 2px; left: 0; right: 0;
  text-align: center;
  font-size: 0.55rem;
  font-weight: 700;
  color: rgba(0,0,0,0.7);
  text-transform: uppercase;
}

/* ============================================================
   FACE NET
   ============================================================ */
.face-net-wrapper { display: flex; justify-content: center; }
.face-net {
  display: grid;
  grid-template-areas: ". U . ." "L F R B" ". D . .";
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  width: 100%;
  max-width: 400px;
}
.face-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; width: 100%; }
.face-grid.U { grid-area: U; }
.face-grid.R { grid-area: R; }
.face-grid.F { grid-area: F; }
.face-grid.D { grid-area: D; }
.face-grid.L { grid-area: L; }
.face-grid.B { grid-area: B; }

.facelet {
  width: 100%; aspect-ratio: 1;
  border-radius: 2px;
  cursor: pointer;
  border: 1px solid rgba(0,0,0,0.4);
  transition: transform 0.1s, box-shadow 0.1s;
  position: relative;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.facelet:hover { transform: scale(1.08); box-shadow: 0 0 8px rgba(255,255,255,0.4); z-index: 2; }
.facelet:active { transform: scale(0.92); }
.facelet.center { cursor: default; }
.facelet.center::after { content: ''; position: absolute; inset: 25%; border-radius: 50%; background: rgba(0,0,0,0.3); }

/* ============================================================
   SOLUTION BOX
   ============================================================ */
#solution-box {
  background: #000;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 12px;
  font-size: 0.8rem;
  line-height: 1.8;
  color: var(--muted);
  word-break: break-all;
  overflow-y: auto;
  margin-bottom: 8px;
}
#solution-box.has-solution { color: #ccc; }
#solution-box.error { color: #ff6666; }

.move-token {
  display: inline-block;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 2px 8px;
  margin: 3px;
  cursor: pointer;
  transition: all 0.15s;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.move-token:hover { background: rgba(255,255,255,0.1); }
.move-token.current { background: var(--accent); border-color: var(--accent); color: #000; font-weight: 700; }
.move-token.done { opacity: 0.3; }

/* ============================================================
   PLAYBACK
   ============================================================ */
.playback-controls { display: flex; justify-content: space-between; margin-bottom: 12px; gap: 8px; }
.playback-controls .btn { flex: 1; text-align: center; }
.step-counter { font-size: 0.7rem; color: var(--muted); }

.progress-bar { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); width: 0%; transition: width 0.3s ease; }

.speed-row { display: flex; align-items: center; font-size: 0.65rem; color: var(--muted); }
input[type=range] {
  -webkit-appearance: none;
  flex: 1; margin: 0 10px;
  height: 2px; background: var(--border);
  border-radius: 2px; outline: none;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%; background: var(--accent); cursor: pointer;
}

/* ============================================================
   RIGHT PANEL / 3D CANVAS
   ============================================================ */
.right-panel { flex: 1; position: relative; overflow: hidden; display: flex; flex-direction: column; }
#three-canvas { flex: 1; display: block; width: 100%; cursor: grab; }
#three-canvas:active { cursor: grabbing; }

.canvas-overlay {
  position: absolute; bottom: 16px; right: 16px;
  display: flex; gap: 8px; flex-direction: column; align-items: flex-end;
}
.hint-text { font-size: 0.55rem; color: #999; letter-spacing: 2px; text-transform: uppercase; text-shadow: 0 1px 4px #000; }

.loading-overlay {
  position: absolute; inset: 0;
  background: rgba(5,5,5,0.95);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px; z-index: 100;
  transition: opacity 0.5s;
}
.loading-overlay.hidden { opacity: 0; pointer-events: none; }
.spinner {
  width: 40px; height: 40px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-family: 'Bebas Neue', sans-serif; font-size: 1rem; letter-spacing: 3px; color: var(--accent); }

/* ============================================================
   SCROLLBAR
   ============================================================ */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: #555; }

/* ============================================================
   MOBILE TAB BAR
   ============================================================ */
/* Hidden on desktop, shown in mobile media query */
.setup-solve-btn { display: none; }

.mobile-tab-bar {
  display: none;
  flex: 0 0 auto;
  background: rgba(5,5,5,0.98);
  border-top: 1px solid var(--border);
  z-index: 20;
}

.mobile-tab-bar-inner { display: flex; }

.m-tab {
  flex: 1;
  padding: 10px 4px;
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  color: var(--muted);
  font-family: 'Space Mono', monospace;
  font-size: 0.55rem;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-height: 54px;
}
.m-tab svg { transition: opacity 0.2s; opacity: 0.4; }
.m-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.m-tab.active svg { opacity: 1; }

/* Scan tab — subtle camera highlight when tapped */
.m-tab--scan:active { color: #7ecfff; }
.m-tab--scan:active svg { opacity: 1; stroke: #7ecfff; }

/* ============================================================
   CAMERA SCANNER OVERLAY
   ============================================================ */
#scanner-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #050505;
  display: flex;
  flex-direction: column;
  font-family: 'Space Mono', monospace;
  color: var(--text);
  /* slide up animation */
  animation: scannerSlideUp 0.28s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes scannerSlideUp {
  from { transform: translateY(100%); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* ── Scanner header ── */
#scanner-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(5,5,5,0.95);
  backdrop-filter: blur(10px);
}
#scanner-title {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.5rem;
  letter-spacing: 4px;
  color: var(--accent);
}
#scanner-close {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font-family: 'Space Mono', monospace;
  font-size: 0.65rem;
  letter-spacing: 2px;
  padding: 7px 14px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}
#scanner-close:hover { border-color: var(--accent); color: var(--accent); }
#scanner-close:active { opacity: 0.6; }

/* ── Scanner body ── */
#scanner-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 16px;
  overflow: hidden;
}

/* ── Viewfinder ── */
#scanner-viewfinder {
  position: relative;
  width: min(76vw, 320px);
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  /* double ring: white inner + subtle glow outer */
  box-shadow:
    0 0 0 2px #fff,
    0 0 0 4px rgba(255,255,255,0.08),
    0 8px 40px rgba(0,0,0,0.7);
  flex-shrink: 0;
}

#scanner-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

#scanner-canvas { display: none; }

/* ── 3×3 grid overlay ── */
#scanner-grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}

.sg-cell {
  border: 1px solid rgba(255,255,255,0.20);
  position: relative;
  transition: background 0.2s;
}

/* Corner accent lines (top-left & bottom-right) */
#scanner-grid::before,
#scanner-grid::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border-color: #fff;
  border-style: solid;
  border-width: 0;
  pointer-events: none;
  z-index: 4;
}
#scanner-grid::before {
  top: 6px; left: 6px;
  border-top-width: 3px;
  border-left-width: 3px;
  border-radius: 2px 0 0 0;
}
#scanner-grid::after {
  bottom: 6px; right: 6px;
  border-bottom-width: 3px;
  border-right-width: 3px;
  border-radius: 0 0 2px 0;
}

/* Color dot shown in center of each cell */
.sg-cell .color-dot {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid rgba(0,0,0,0.45);
  box-shadow: 0 2px 8px rgba(0,0,0,0.55);
  background: #222;
  transition: background 0.18s;
}

/* Face key label inside each cell */
.sg-cell .face-label {
  position: absolute;
  bottom: 3px;
  right: 5px;
  font-size: 0.5rem;
  font-weight: 700;
  color: rgba(255,255,255,0.75);
  text-shadow: 0 1px 3px #000;
  letter-spacing: 1px;
}

/* ── Face selector pills ── */
#face-selector {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.fs-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font-family: 'Space Mono', monospace;
  font-size: 0.6rem;
  letter-spacing: 2px;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.fs-btn .fs-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  border: 1px solid rgba(0,0,0,0.3);
  flex-shrink: 0;
}
.fs-btn.selected  { border-color: #fff; color: #fff; }
.fs-btn.done-face { opacity: 0.45; }
.fs-btn:active    { opacity: 0.65; }

/* ── Instruction line ── */
#scanner-instruction {
  font-size: 0.6rem;
  letter-spacing: 1.5px;
  color: var(--muted);
  text-align: center;
  line-height: 1.65;
  padding: 0 8px;
}

/* ── Progress dots ── */
#scanner-progress {
  display: flex;
  gap: 8px;
  align-items: center;
}

.sp-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  border: 1px solid #3a3a3a;
  background: #111;
  transition: all 0.2s;
}
.sp-dot.done   { background: #fff; border-color: #fff; }
.sp-dot.active {
  background: transparent;
  border-color: #fff;
  box-shadow: 0 0 0 2px rgba(255,255,255,0.25);
}

/* ── Action buttons ── */
#scanner-actions {
  display: flex;
  gap: 10px;
  width: 100%;
}

#btn-capture {
  flex: 1;
  height: 50px;
  border-radius: 6px;
  cursor: pointer;
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #000;
  font-family: 'Space Mono', monospace;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 3px;
  transition: opacity 0.15s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}
#btn-capture:active { opacity: 0.7; }
#btn-capture.apply-mode {
  background: #7ecfff;
  border-color: #7ecfff;
}

#btn-retake {
  flex: 0 0 auto;
  height: 50px;
  padding: 0 16px;
  border-radius: 6px;
  cursor: pointer;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font-family: 'Space Mono', monospace;
  font-size: 0.7rem;
  letter-spacing: 2px;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}
#btn-retake:hover  { border-color: #555; color: #aaa; }
#btn-retake:active { opacity: 0.6; }

/* ── Error banner ── */
#scanner-error {
  display: none;
  width: 100%;
  background: rgba(255, 50, 50, 0.10);
  border: 1px solid rgba(255, 50, 50, 0.35);
  color: #ff8888;
  font-size: 0.6rem;
  letter-spacing: 1px;
  border-radius: 4px;
  padding: 8px 14px;
  text-align: center;
  line-height: 1.55;
}
#scanner-error.visible { display: block; }

/* ── Scan-success flash on viewfinder ── */
#scanner-viewfinder.captured::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.18);
  animation: captureFlash 0.35s ease-out forwards;
  pointer-events: none;
  z-index: 10;
}
@keyframes captureFlash {
  0%   { opacity: 1; }
  100% { opacity: 0; }
}

/* ============================================================
   MOBILE RESPONSIVE  ≤ 900px
   ============================================================ */
@media (max-width: 900px) {
  body { height: 100dvh; overflow: hidden; }

  header { padding: 8px 14px; }
  header h1 { font-size: 1.2rem; letter-spacing: 2px; }
  .github-link span { display: none; }

  /* Desktop scan button hidden on mobile */
  .scan-desktop-btn { display: none; }

  .mobile-tab-bar { display: block; }

  .layout {
    flex-direction: column;
    overflow: hidden;
    flex: 1;
    min-height: 0;
  }

  /* 3D viewer */
  .right-panel {
    order: -1;
    flex: 0 0 260px;
    border-bottom: 1px solid var(--border);
    border-right: none;
  }
  .canvas-overlay { bottom: 8px; right: 10px; }
  .hint-text { display: none; }

  /* Scrollable content pane */
  .left-panel {
    width: 100%;
    flex: 1;
    min-height: 0;
    border-right: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* All sections hidden; JS shows active ones */
  .section { display: none; }
  .section.m-visible { display: flex; }

  /* ---- SETUP TAB ---- */
  .section[data-s="scramble"] { padding: 14px 14px 10px; }

  .scramble-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .scramble-row input {
    grid-column: 1 / -1;
    height: 50px;
    font-size: 0.8rem;
    padding: 10px 12px;
  }
  .scramble-row .btn { height: 50px; font-size: 0.8rem; }

  .section[data-s="paint"] { padding: 12px 14px 16px; }

  .setup-solve-btn {
    display: flex;
    width: 100%;
    margin-top: 16px;
    height: 52px;
    font-size: 0.9rem;
    letter-spacing: 3px;
    border-radius: 6px;
  }

  .color-palette {
    gap: 7px;
    justify-content: space-between;
    margin-bottom: 14px;
  }
  .color-swatch {
    flex: 1;
    max-width: 46px;
    height: 46px;
    border-radius: 8px;
  }
  .color-swatch span { font-size: 0.6rem; }

  .face-net-wrapper { width: 100%; }
  .face-net { max-width: 100%; gap: 5px; }
  .face-grid { gap: 3px; }
  .facelet { border-radius: 3px; border-width: 1.5px; }

  .btn.mini { min-height: 36px; padding: 6px 12px; font-size: 0.65rem; }

  /* ---- SOLVE TAB ---- */
  .section[data-s="solve"] { padding: 14px 14px 10px; }

  #solve-btn {
    display: inline-flex !important;
    height: 44px;
    font-size: 0.8rem;
    padding: 0 18px;
    letter-spacing: 2px;
  }

  #solution-box {
    font-size: 0.78rem;
    max-height: 160px;
    line-height: 2;
  }

  .move-token {
    padding: 5px 11px;
    margin: 3px;
    font-size: 0.75rem;
  }

  .section[data-s="playback"] { padding: 14px 14px; }

  .playback-controls { gap: 8px; }
  .playback-controls .btn { height: 56px; font-size: 0.8rem; border-radius: 6px; }

  .speed-row { margin-top: 6px; }
  input[type=range] { height: 4px; }
  input[type=range]::-webkit-slider-thumb { width: 24px; height: 24px; }

  /* ---- FACELET TAB ---- */
  .section[data-s="facelet"] { padding: 14px 14px; }
  #facelet-string { height: 50px; font-size: 0.72rem; padding: 12px; }

  /* ---- SCANNER OVERLAY on mobile ---- */
  #scanner-body { gap: 12px; padding: 12px 14px; }

  #scanner-viewfinder {
    width: min(78vw, 300px);
  }

  #btn-capture { height: 54px; font-size: 0.85rem; }
  #btn-retake  { height: 54px; }

  .fs-btn { padding: 7px 10px; font-size: 0.58rem; }
}

@media (max-width: 380px) {
  .right-panel { flex: 0 0 220px; }
  header h1 { font-size: 1rem; }
  .color-swatch { height: 40px; }
  .scramble-row .btn { font-size: 0.7rem; }

  #scanner-viewfinder { width: min(82vw, 280px); }
  #btn-capture { font-size: 0.75rem; letter-spacing: 2px; }
}