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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f6f4ef;
  color: #222;
  overflow: hidden;
  /* Prevent iOS double-tap zoom and pinch-zoom across the whole app */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  height: 100vh;
}

#canvas {
  flex: 1;
  position: relative;
  background: #fbfaf6;
  overflow: hidden;
  user-select: none;
}

#sidebar {
  width: 260px;
  flex-shrink: 0;
  background: #ffffff;
  border-left: 1px solid #e6e3dc;
  display: flex;
  flex-direction: column;
  padding: 12px;
}

/* ---- Library sidebar (Phase 3, Plan 02) ---- */
.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.library-counter {
  font-size: 13px;
  font-weight: 600;
  color: #666;
}

.clear-canvas-btn {
  appearance: none;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  color: #555;
  background: #faf8f2;
  border: 1px solid #e0dcd2;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  text-align: center;
}
.clear-canvas-btn:hover,
.clear-canvas-btn:focus-visible {
  background: #f1ede2;
  border-color: #c9c3b4;
  outline: none;
}

.library-search {
  width: 100%;
  font: inherit;
  font-size: 16px; /* 16px+ prevents iOS auto-zoom on focus */
  padding: 6px 8px;
  border: 1px solid #e0dcd2;
  border-radius: 6px;
  background: #fafaf6;
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
.library-search:focus {
  border-color: #c9c3b4;
  background: #fff;
}

.library-grid {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 6px;
  align-content: start;
}

.tile {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  padding: 4px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 120ms ease, border-color 120ms ease;
}
.tile:hover,
.tile:focus-visible {
  background: #f1ede2;
  border-color: #e0dcd2;
  outline: none;
}
.tile-glyph {
  font-size: 28px;
  line-height: 1;
  pointer-events: none;
}

/* ---- Finished tile (Phase 5, FIN-02) ---- */
.tile.finished {
  opacity: 0.45;
  filter: saturate(0.5);
}
.tile.finished:hover,
.tile.finished:focus-visible {
  opacity: 0.7;
}

/* ---- Sidebar tabs (Phase 5, GRAPH-01) ---- */
.sidebar-tabs {
  display: flex;
  gap: 4px;
  margin: 4px 0;
}
.tab-btn {
  appearance: none;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  color: #666;
  background: #faf8f2;
  border: 1px solid #e0dcd2;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  flex: 1;
}
.tab-btn:hover { background: #f1ede2; }
.tab-btn.active {
  background: #fff;
  color: #222;
  border-color: #c9c3b4;
  box-shadow: inset 0 -2px 0 #c9c3b4;
}

/* Hide library-only controls when Graph tab is active. */
.sidebar-graph-active .library-only { display: none; }

.panel[hidden] { display: none; }

/* ---- Graph view (Phase 5 + v1.1 force-directed) ---- */
.graph-panel {
  flex: 1;
  min-height: 0;
  position: relative;
  padding: 0;
  overflow: hidden;
  background: #fdfcf8;
  border: 1px solid #ece8dd;
  border-radius: 8px;
}

.graph-toolbar {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 3;
  display: flex;
  gap: 4px;
}

.graph-expand-btn {
  appearance: none;
  font: inherit;
  font-size: 14px;
  padding: 4px 8px;
  background: #fff;
  border: 1px solid #e0dcd2;
  border-radius: 6px;
  cursor: pointer;
  color: #555;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.graph-expand-btn:hover { background: #faf8f2; }

/* Fullscreen graph overlay */
body.graph-fullscreen #sidebar {
  position: fixed;
  inset: 0;
  width: 100vw;
  max-height: 100dvh;
  height: 100dvh;
  z-index: 50;
  padding: 12px;
  border: none;
  background: #fdfcf8;
}
body.graph-fullscreen .graph-panel { min-height: 0; flex: 1; }
body.graph-fullscreen .log-panel { display: none; }

.graph-svg {
  display: block;
  width: 100%;
  height: 100%;
  user-select: none;
  touch-action: none;
}

.graph-svg .gnode circle {
  fill: #ffffff;
  stroke: #c9c3b4;
  stroke-width: 1.5;
  transition: fill 120ms ease, stroke 120ms ease, stroke-width 120ms ease;
}
.graph-svg .gnode.starter circle {
  fill: #fff4d6;
  stroke: #d9b76d;
}
.graph-svg .gnode text {
  font-size: 22px;
  pointer-events: none;
}
.graph-svg .gnode { cursor: pointer; transition: opacity 180ms ease; }

.graph-svg .graph-edges line {
  stroke: #b9b3a4;
  stroke-width: 1.2;
  opacity: 0.5;
  transition: stroke 140ms ease, stroke-width 140ms ease, opacity 140ms ease;
}

/* Highlight state */
.graph-svg.has-highlight .graph-edges line { opacity: 0.12; }
.graph-svg.has-highlight .graph-edges line.is-connected {
  stroke: #d96b4a;
  stroke-width: 2.2;
  opacity: 0.95;
}
.graph-svg.has-highlight .gnode.is-faded { opacity: 0.25; }
.graph-svg.has-highlight .gnode.is-connected circle {
  stroke: #d96b4a;
  stroke-width: 2;
}
.graph-svg.has-highlight .gnode.is-hover circle {
  stroke: #d96b4a;
  stroke-width: 2.5;
  fill: #ffe8de;
}

/* Legacy classes (no longer used by force layout) */
.graph-tier { display: none; }
.graph-node { display: none; }

.graph-edges-legacy line {
  stroke: #c9c3b4;
  stroke-width: 1;
  opacity: 0.6;
}

/* ---- Discovery log (Phase 3, Plan 03) ---- */
.log-panel {
  position: fixed;
  left: 0;
  right: 260px;
  bottom: 0;
  background: #ffffff;
  border-top: 1px solid #e6e3dc;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
  z-index: 20;
  display: flex;
  flex-direction: column;
  max-height: 40vh;
}

.log-header {
  display: flex;
  align-items: center;
  padding: 0;
}

.log-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  appearance: none;
  border: none;
  background: transparent;
  padding: 8px 12px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  color: #555;
  text-align: left;
  cursor: pointer;
}
.log-toggle:hover { background: #faf8f2; }

.log-chevron {
  display: inline-block;
  transition: transform 150ms ease;
  font-size: 12px;
  color: #888;
}
.log-panel.collapsed .log-chevron {
  transform: rotate(-90deg);
}

.log-title {
  font-size: 13px;
}

.log-entries {
  list-style: none;
  margin: 0;
  padding: 0 12px 10px;
  overflow-y: auto;
  max-height: calc(40vh - 36px);
}

.log-panel.collapsed .log-entries {
  display: none;
}

.log-entry {
  padding: 4px 0;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: #333;
  border-bottom: 1px solid #f2efe7;
}
.log-entry:last-child { border-bottom: none; }

#canvas {
  padding-bottom: 44px;
}

.emoji {
  position: absolute;
  font-size: 48px;
  line-height: 1;
  cursor: grab;
  user-select: none;
  touch-action: none;
  transition: transform 80ms ease;
  will-change: transform;
}

.emoji.dragging {
  cursor: grabbing;
  transition: none;
  z-index: 10;
}

/* ---- Fusion miss shake (Phase 2) ---- */
.emoji.shake {
  animation: shake 0.4s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

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

/* ---- Mobile layout (<= 720px) ---- */
@media (max-width: 720px) {
  html, body { height: 100dvh; }
  body {
    flex-direction: column;
    height: 100dvh;
  }

  #canvas {
    flex: 1 1 auto;
    min-height: 0;
    padding-bottom: 0;
  }

  /* Smaller emoji so more fit on a phone canvas */
  .emoji { font-size: 40px; }

  /* Sidebar becomes a bottom drawer */
  #sidebar {
    width: 100%;
    max-height: 40dvh;
    flex: 0 0 auto;
    border-left: none;
    border-top: 1px solid #e6e3dc;
    padding: 8px 10px;
    box-shadow: 0 -2px 8px rgba(0,0,0,0.06);
  }

  .sidebar-header {
    gap: 4px;
    margin-bottom: 6px;
  }

  .clear-canvas-btn, .tab-btn {
    padding: 8px 10px;
    font-size: 13px;
  }

  /* Library grid: slightly smaller tiles, scroll vertically inside the drawer */
  .library-grid {
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: 4px;
    max-height: calc(40dvh - 110px);
  }

  .tile { padding: 2px; }
  .tile-glyph { font-size: 24px; }

  /* Graph view also capped to the drawer height */
  .graph-panel { max-height: calc(40dvh - 100px); }
  .graph-node { width: 30px; height: 30px; font-size: 20px; }

  /* Log floats ABOVE the sidebar, full width */
  .log-panel {
    right: 0;
    bottom: 40dvh;
    max-height: 28dvh;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    border-bottom: 1px solid #e6e3dc;
    border-top: 1px solid #e6e3dc;
  }
  .log-entries { max-height: calc(28dvh - 36px); }
  .log-panel.collapsed { max-height: 36px; }
}

/* ---- Very small phones ---- */
@media (max-width: 380px) {
  .library-grid { grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); }
  .tile-glyph { font-size: 22px; }
  .emoji { font-size: 36px; }
}
