/* ==========================================================================
   ZAYPEN — Expandable Piano Tool Dock, Portrait/Landscape Sizing & Touch Lock
   ========================================================================== */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-drag: none;
}

html, body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ffffff;
  touch-action: none;
  overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
}

#app {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #ffffff;
  touch-action: none;
}

/* Fullscreen Drawing & Animation Canvases */
#draw-canvas,
#fx-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#draw-canvas {
  z-index: 1;
  background: #ffffff;
  cursor: crosshair;
}

#fx-canvas {
  z-index: 2;
  pointer-events: none;
}

/* Top-Left Music Toggle Pill */
#top-left-actions {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  left: calc(env(safe-area-inset-left, 0px) + 12px);
  z-index: 20;
  display: flex;
  align-items: center;
  padding: 5px;
  border-radius: 999px;
  background: rgba(248, 250, 252, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(15, 23, 42, 0.08);
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.05);
  touch-action: none;
}

/* Top-Right Sleek Action Bar (Undo, Redo, and BIG Smiley New Page Button) */
#top-actions {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 999px;
  background: rgba(248, 250, 252, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(15, 23, 42, 0.08);
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.05);
  touch-action: none;
}

.action-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: #ffffff;
  color: #1e293b;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.09);
  transition: transform 0.16s cubic-bezier(0.34, 1.56, 0.64, 1),
              background-color 0.15s ease,
              color 0.15s ease,
              opacity 0.15s ease;
  touch-action: none;
}

.action-btn svg {
  width: 24px;
  height: 24px;
  pointer-events: none;
}

.action-btn-music {
  width: 44px;
  height: 44px;
  color: #64748b;
  background: #f8fafc;
}

.action-btn-music.active {
  color: #0284c7;
  background: #e0f2fe;
}

.action-btn-music svg {
  width: 22px;
  height: 22px;
}

.action-btn:not(:disabled):active,
.action-btn.pop {
  transform: scale(0.88);
}

.action-btn:disabled {
  opacity: 0.28;
  cursor: default;
  box-shadow: none;
}

/* BIG Smiley Face New Page Button */
.action-btn-clear {
  width: 60px;
  height: 60px;
  padding: 0;
  background: transparent;
  box-shadow: 0 4px 12px rgba(234, 179, 8, 0.35);
}

.action-btn-clear svg {
  width: 58px;
  height: 58px;
}

.action-btn-clear:disabled {
  opacity: 0.45;
  filter: saturate(0.75);
  box-shadow: none;
}

.action-btn-clear:not(:disabled):hover {
  transform: scale(1.06);
}

.action-btn-clear.clearing {
  animation: smiley-bounce 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes smiley-bounce {
  0% { transform: scale(1) rotate(0deg); }
  30% { transform: scale(1.22) rotate(12deg); }
  65% { transform: scale(0.92) rotate(-8deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* ==========================================================================
   Expandable Bottom Tool Dock & Swipeable Colour Piano
   ========================================================================== */

:root {
  /* Default / Landscape & Desktop — sleek & proportional so drawing area stays huge */
  --dock-height: 68px;
  --tool-height: 64px;
  --tool-pencil-height: 60px;
  --rubber-height: 72px;
  --rubber-width: 54px;
  --tool-rest-y: 14px;
  --tool-active-y: 2px;
  --collapsed-tool-width: 54px;
}

/* Portrait Tablet / iPad (wide enough to fit all 15 tools comfortably at the bottom) */
@media (orientation: portrait) and (min-width: 681px) {
  :root {
    --dock-height: 76px;
    --tool-height: 70px;
    --tool-pencil-height: 66px;
    --rubber-height: 78px;
    --rubber-width: 58px;
    --tool-rest-y: 15px;
    --tool-active-y: 2px;
    --collapsed-tool-width: 56px;
  }
}

/* Extra-compact height on short horizontal phone screens */
@media (orientation: landscape) and (max-height: 520px) {
  :root {
    --dock-height: 52px;
    --tool-height: 48px;
    --tool-pencil-height: 44px;
    --rubber-height: 54px;
    --rubber-width: 44px;
    --tool-rest-y: 11px;
    --tool-active-y: 2px;
    --collapsed-tool-width: 46px;
  }
}

#pencil-dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  height: calc(var(--dock-height) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: max(6px, env(safe-area-inset-left, 6px));
  padding-right: max(6px, env(safe-area-inset-right, 6px));
  background: transparent;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  pointer-events: none;
  touch-action: none;
}

#pencil-row {
  width: 100%;
  max-width: 1240px;
  height: var(--dock-height);
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  gap: clamp(2px, 0.5vw, 8px);
  padding: 0 4px;
  pointer-events: none;
  touch-action: none;
}

#pencil-row.is-expanded {
  justify-content: flex-end;
}

/* Expanded 10-Colour Piano Strip (pulls out to the left from the bottom-right tools!) */
.piano-strip {
  flex: 1 1 auto;
  height: var(--dock-height);
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: clamp(1px, 0.35vw, 5px);
  padding-right: clamp(4px, 0.8vw, 12px);
  border-right: 1.5px solid rgba(15, 23, 42, 0.09);
  pointer-events: auto;
  touch-action: none;
  animation: strip-roll-out 0.24s cubic-bezier(0.34, 1.56, 0.64, 1);
  transform-origin: right bottom;
}

@keyframes strip-roll-out {
  0% {
    opacity: 0;
    transform: scaleX(0.35) translateY(16px);
  }
  100% {
    opacity: 1;
    transform: scaleX(1) translateY(0);
  }
}

/* Core Tool Switcher Group — stays anchored in the exact same bottom-right spot when expanded or retracted! */
.core-switcher {
  flex: 0 0 auto;
  height: var(--dock-height);
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  gap: clamp(2px, 0.45vw, 6px);
  pointer-events: auto;
  touch-action: none;
}

/* Individual Tool Button */
.tool-btn {
  position: relative;
  border: none;
  background: transparent;
  padding: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  cursor: pointer;
  touch-action: none;
  outline: none;
  height: var(--tool-height);
  transform: translateY(var(--tool-rest-y));
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
              filter 0.18s ease;
  filter: drop-shadow(0 2px 3px rgba(15, 23, 42, 0.15));
  will-change: transform;
}

/* Piano key inside the expanded 10-colour strip */
.piano-strip .tool-btn {
  flex: 1 1 0;
  min-width: 0;
  max-width: 72px;
}

/* Core tool buttons keep identical width and position in both collapsed and expanded states */
.core-switcher .tool-btn:not(.tool-btn-rubber) {
  width: calc(var(--collapsed-tool-width) * 0.72);
}

.core-switcher .tool-btn.active-family:not(.tool-btn-rubber) {
  opacity: 1;
}

.tool-btn-pencil {
  height: var(--tool-pencil-height);
}

/* Selected tool / colour sticks up slightly! */
.tool-btn.active {
  transform: translateY(var(--tool-active-y));
  filter: drop-shadow(0 5px 9px rgba(15, 23, 42, 0.26));
  z-index: 2;
}

/* Piano note bounce when swiped or tapped */
.tool-btn.piano-pop {
  transform: translateY(calc(var(--tool-active-y) - 7px)) scale(1.06);
}

/* Big Obvious Rubber at the right end — fixed size so nothing shifts on retract/expand */
.tool-btn-rubber {
  flex: 0 0 auto;
  width: calc(var(--rubber-width) * 0.88);
  height: var(--rubber-height);
  margin-left: clamp(3px, 0.6vw, 8px);
}

.tool-btn svg {
  width: auto;
  height: 100%;
  flex-shrink: 0;
  overflow: visible;
  pointer-events: none;
  display: block;
}

@media (max-width: 560px) {
  #top-left-actions {
    top: calc(env(safe-area-inset-top, 0px) + 8px);
    left: calc(env(safe-area-inset-left, 0px) + 8px);
    padding: 4px;
  }

  #top-actions {
    top: calc(env(safe-area-inset-top, 0px) + 8px);
    right: calc(env(safe-area-inset-right, 0px) + 8px);
    gap: 6px;
    padding: 5px 6px;
  }

  .action-btn {
    width: 38px;
    height: 38px;
  }

  .action-btn-music {
    width: 38px;
    height: 38px;
  }

  .action-btn-clear {
    width: 48px;
    height: 48px;
  }

  .action-btn-clear svg {
    width: 46px;
    height: 46px;
  }
}

/* ==========================================================================
   Vertical Phone Mode — Side-of-Screen 10-Colour Piano Strip & Compact Dock
   ========================================================================== */
@media (orientation: portrait) and (max-width: 680px) {
  :root {
    --dock-height: 58px;
    --tool-height: 54px;
    --tool-pencil-height: 50px;
    --rubber-height: 60px;
    --rubber-width: 44px;
    --tool-rest-y: 12px;
    --tool-active-y: 2px;
    --collapsed-tool-width: 48px;
  }

  .piano-strip {
    position: fixed;
    right: 0;
    top: calc(env(safe-area-inset-top, 0px) + 68px);
    bottom: calc(var(--dock-height) + env(safe-area-inset-bottom, 0px) + 12px);
    width: var(--tool-height);
    height: auto;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
    gap: 2px;
    padding: 4px 0;
    border-right: none;
    overflow: hidden;
    animation: strip-roll-out-side 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
    transform-origin: right bottom;
  }

  @keyframes strip-roll-out-side {
    0% {
      opacity: 0;
      transform: scaleY(0.35) translateX(18px);
    }
    100% {
      opacity: 1;
      transform: scaleY(1) translateX(0);
    }
  }

  .piano-strip .tool-btn {
    flex: 1 1 0;
    width: var(--tool-height);
    max-width: var(--tool-height);
    height: auto;
    max-height: 48px;
    align-items: center;
    justify-content: center;
    transform: translateX(var(--tool-rest-y));
  }

  .piano-strip .tool-btn-pencil {
    width: var(--tool-pencil-height);
    max-width: var(--tool-pencil-height);
  }

  .piano-strip .tool-btn svg {
    position: absolute;
    width: auto;
    height: var(--tool-height);
    transform: rotate(-90deg);
  }

  .piano-strip .tool-btn-pencil svg {
    height: var(--tool-pencil-height);
  }

  .piano-strip .tool-btn.active {
    transform: translateX(var(--tool-active-y));
  }

  .piano-strip .tool-btn.piano-pop {
    transform: translateX(1px) scale(1.04);
  }
}
