@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@300;400;500&family=IBM+Plex+Sans:wght@300;400;500&display=swap');

@import url('https://fonts.googleapis.com/css2?family=Geist:wght@100..900&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Geist+Mono:wght@100..900&display=swap');


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

  :root {
    --bg: #1a1a1a;
    --panel: #2a2a2a;
    --panel-light: #333;
    --border: #444;
    --text: #e0e0e0;
    --text-dim: #888;
    --accent: #ff4136;
    --accent-blue: #4a6cf7;
    --btn-bg: #3a3a3a;
    --btn-active: #4a4a4a;
    --font: "Geist Mono", monospace;
  }

  body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-weight: 300;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    user-select: none;
  }

  /* TOP TOOLBAR */
  #toolbar {
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    height: 52px;
    display: flex;
    align-items: center;
    padding: 0 12px;
    gap: 6px;
    flex-shrink: 0;
  }

  .tool-btn {
    width: 36px;
    height: 36px;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    position: relative;
  }
  .tool-btn:hover { background: var(--btn-active); border-color: #666; }
  .tool-btn.active { border-color: var(--accent-blue); background: rgba(74,108,247,0.15); }

  .tool-btn svg { pointer-events: none; }

  .toolbar-sep { width: 1px; height: 28px; background: var(--border); margin: 0 4px; }

  #draw-tools, #record-tools {
    display: flex;
    align-items: center;
    gap: 6px;
  }
  .hidden { display: none !important; }

  /* Color picker */
  #color-btn {
    width: 36px; height: 36px;
    border-radius: 6px;
    border: 2px solid var(--border);
    cursor: pointer;
    transition: border-color 0.15s;
    padding: 0;
    overflow: hidden;
    position: relative;
  }
  #color-btn:hover { border-color: #666; }
  #color-input {
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    border: none; padding: 0;
    cursor: pointer;
    opacity: 0;
  }
  #color-preview {
    width: 100%; height: 100%;
    background: #ff4136;
    pointer-events: none;
  }

  /* Size selector */
  #size-select {
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-family: var(--font);
    font-size: 11px;
    padding: 0 8px;
    height: 36px;
    cursor: pointer;
    outline: none;
  }

  /* Export btn top-right */
  #export-btn {
    margin-left: auto;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-family: var(--font);
    font-size: 11px;
    padding: 0 14px;
    height: 36px;
    cursor: pointer;
    display: flex; align-items: center; gap: 6px;
    transition: all 0.15s;
  }
  #export-btn:hover { background: var(--btn-active); border-color: #666; }
  #export-btn:disabled { opacity: 0.4; cursor: not-allowed; }

  /* CANVAS AREA */
  #canvas-area {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: #111;   /* dark surround so the canvas boundary is clear */
    cursor: default;
  }

  /* Canvas is centred and sized by JS to match the chosen aspect ratio */
  #main-canvas {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 4px 32px rgba(0,0,0,0.5);
    cursor: crosshair;
    touch-action: none; /* prevent browser scroll/zoom on touch */
  }

  /* Recording overlay */
  #rec-overlay {
    position: absolute;
    top: 12px; right: 12px;
    background: rgba(255,65,54,0.9);
    color: white;
    font-family: var(--font);
    font-size: 12px;
    padding: 6px 12px;
    border-radius: 20px;
    display: none;
    align-items: center;
    gap: 6px;
    pointer-events: none;
  }
  #rec-overlay.visible { display: flex; }
  .rec-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: white;
    animation: blink 1s infinite;
  }
  @keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.2} }

  /* Status bar */
  #status {
    position: absolute;
    bottom: 12px; left: 12px;
    font-family: var(--font);
    font-size: 11px;
    color: #999;
    pointer-events: none;
  }

  /* BOTTOM PANEL */
  #bottom-panel {
    background: var(--panel);
    border-top: 1px solid var(--border);
    padding: 12px 16px;
    padding-bottom: max(12px, calc(12px + env(safe-area-inset-bottom)));
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  /* Layer tabs row */
  #layer-row {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 1;
    min-width: 0;
  }

  /* "LAYERS" label */
  .row-label {
    font-family: var(--font);
    font-size: 11px;
    color: var(--text-dim);
    margin-right: 2px;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .layer-tab {
    height: 36px;
    padding: 0 14px;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 11px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
  }
  .layer-tab:hover { background: var(--btn-active); color: var(--text); }
  .layer-tab.active { border-color: var(--text); color: var(--text); background: var(--btn-active); }
  .layer-tab.has-shape { border-color: var(--accent-blue); color: var(--text); }
  .layer-tab.has-animation { border-color: var(--accent); color: var(--text); }
  .layer-tab.dragging { opacity: 0.35; cursor: grabbing; }
  .layer-tab.drag-over { border-color: var(--accent-blue); background: rgba(74,108,247,0.2); }
  .layer-tab[draggable="true"] { cursor: grab; }

  #add-layer-btn {
    height: 36px;
    padding: 0 10px;
    background: none;
    border: 1px dashed var(--border);
    border-radius: 6px;
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 11px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
    flex-shrink: 0;
  }
  #add-layer-btn:hover { border-color: #666; color: var(--text); }

  .ctrl-btn {
    width: 36px; height: 36px;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
  }
  .ctrl-btn:hover { background: var(--btn-active); }
  .ctrl-btn.rec-btn { border-color: var(--accent); background: rgba(255,65,54,0.15); }
  .ctrl-btn.rec-btn:hover { background: rgba(255,65,54,0.3); }
  .ctrl-btn.rec-btn.recording { background: var(--accent); }

  /* "PRESS R" label inside record button — desktop only */
  .rec-btn-label {
    font-family: var(--font);
    font-size: 10px;
    color: var(--accent);
    white-space: nowrap;
    pointer-events: none;
  }
  .ctrl-btn.rec-btn {
    width: auto;
    padding: 0 10px;
    gap: 7px;
  }

  /* Timeline */
  #timeline-row {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  #time-start, #time-end {
    font-family: var(--font);
    font-size: 11px;
    color: var(--text-dim);
    min-width: 36px;
  }

  #timeline-track {
    flex: 1;
    height: 20px;
    position: relative;
    cursor: pointer;
  }

  #timeline-bg {
    position: absolute;
    top: 50%; transform: translateY(-50%);
    width: 100%; height: 2px;
    background: var(--border);
    border-radius: 1px;
  }

  #timeline-filled {
    position: absolute;
    top: 50%; transform: translateY(-50%);
    left: 0; height: 2px;
    background: var(--accent);
    border-radius: 1px;
    width: 0%;
    transition: width 0.05s linear;
  }

  #timeline-head {
    position: absolute;
    top: 50%; transform: translate(-50%, -50%);
    width: 14px; height: 14px;
    background: var(--accent);
    border-radius: 50%;
    border: 2px solid #fff;
    left: 0%;
    cursor: grab;
    transition: left 0.05s linear;
  }
  #timeline-head:active { cursor: grabbing; }

  /* Mode buttons */
  #mode-btns {
    display: flex;
    align-items: center;
    gap: 4px;
  }

  .mode-btn {
    height: 36px;
    padding: 0 14px;
    border-radius: 6px;
    font-family: var(--font);
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.05em;
    cursor: pointer;
    transition: all 0.18s ease;
    white-space: nowrap;
    /* inactive / greyed state */
    background: var(--btn-bg);
    border: 1px solid var(--border);
    color: var(--text-dim);
  }
  .mode-btn:hover:not(.active):not(:disabled) {
    background: var(--btn-active);
    border-color: #666;
    color: var(--text);
  }

  /* DRAW active */
  #btn-draw.active {
    border: 1.5px solid rgba(74,108,247,0.6);
    background: rgba(74,108,247,0.15);
    color: #7b9fff;
  }

  /* RECORD active */
  #btn-record.active {
    border: 1.5px solid rgba(255,65,54,0.6);
    background: rgba(255,65,54,0.12);
    color: #ff7a73;
  }

  .mode-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
  }
  #duration-select {
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-family: var(--font);
    font-size: 11px;
    padding: 0 8px;
    height: 36px;
    cursor: pointer;
    outline: none;
  }

  /* Modal overlay */
  #modal {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.7);
    z-index: 100;
    align-items: center;
    justify-content: center;
  }
  #modal.visible { display: flex; }
  #modal-box {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 28px 32px;
    min-width: 280px;
    text-align: center;
  }
  #modal-box h3 { font-family: var(--font); font-size: 14px; margin-bottom: 8px; }
  #modal-box p { font-size: 13px; color: var(--text-dim); margin-bottom: 20px; }
  #modal-progress-bg {
    height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; margin-bottom: 20px;
  }
  #modal-progress { height: 100%; background: var(--accent); width: 0%; transition: width 0.1s; }
  #modal-cancel {
    background: var(--btn-bg); border: 1px solid var(--border); border-radius: 6px;
    color: var(--text); font-family: var(--font); font-size: 12px;
    padding: 8px 20px; cursor: pointer;
  }
  #modal-status {
    font-family: var(--font);
    font-size: 11px;
    color: var(--text-dim);
    margin-bottom: 14px;
    min-height: 16px;
  }
  /* Shape Inspector */
  #inspector {
    display: none;
    position: absolute;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px 16px;
    width: 220px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
    z-index: 50;
    flex-direction: column;
    gap: 12px;
  }
  #inspector.visible { display: flex; }

  #inspector-title {
    font-family: var(--font);
    font-size: 10px;
    letter-spacing: 0.1em;
    color: var(--text-dim);
    margin-bottom: 2px;
  }

  .inspector-row {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .inspector-row label {
    font-family: var(--font);
    font-size: 10px;
    color: var(--text-dim);
    width: 36px;
    flex-shrink: 0;
  }

  #insp-color-wrap {
    position: relative;
    width: 32px; height: 32px;
    border-radius: 6px;
    border: 1.5px solid var(--border);
    overflow: hidden;
    cursor: pointer;
    flex-shrink: 0;
  }
  #insp-color {
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    opacity: 0; cursor: pointer;
  }
  #insp-color-preview {
    position: absolute; inset: 0;
    pointer-events: none;
  }

  #insp-size {
    flex: 1;
    accent-color: var(--accent-blue);
    cursor: pointer;
  }
  #insp-size-val {
    font-family: var(--font);
    font-size: 10px;
    color: var(--text-dim);
    width: 34px;
    text-align: right;
    flex-shrink: 0;
  }

  #insp-close {
    margin-top: 2px;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-family: var(--font);
    font-size: 11px;
    padding: 7px 0;
    cursor: pointer;
    transition: background 0.15s;
  }
  #insp-close:hover { background: var(--btn-active); }

  /* Toolbar right group (info + export) */
  #toolbar-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 6px;
  }
  #export-btn { margin-left: 0; } /* reset the old margin-left:auto */

  /* Info button */
  #info-btn {
    width: 36px; height: 36px;
    background: var(--btn-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
  }
  #info-btn:hover { background: var(--btn-active); border-color: #666; }

  /* Help modal overlay */
  #help-modal {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.65);
    z-index: 200;
    align-items: center;
    justify-content: center;
  }
  #help-modal.visible { display: flex; }

  #help-box {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 22px 28px 24px;
    width: 380px;
    max-width: calc(100vw - 40px);
    display: flex;
    flex-direction: column;
    gap: 14px;
    box-shadow: 0 16px 48px rgba(0,0,0,0.5);
  }

  /* Header row: step label left, ✕ right */
  #help-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  #help-step-label {
    font-family: var(--font);
    font-size: 10px;
    letter-spacing: 0.12em;
    color: var(--text-dim);
  }

  /* ✕ close button in header */
  #help-close {
    width: 26px; height: 26px;
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-dim);
    font-size: 12px;
    line-height: 1;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
    padding: 0;
    flex-shrink: 0;
  }
  #help-close:hover { border-color: #666; color: var(--text); background: var(--btn-bg); }

  #help-title {
    font-family: var(--font);
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    line-height: 1.4;
  }

  #help-body {
    font-family: var(--font);
    font-size: 13px;
    color: #c0c0c0;
    line-height: 1.65;
    min-height: 56px;
  }

  #help-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 4px;
  }

  /* Progress dots */
  #help-dots {
    display: flex;
    gap: 6px;
    align-items: center;
  }
  .help-dot {
    width: 6px; height: 6px;
    border-radius: 50%;
    background: var(--border);
    transition: background 0.2s;
  }
  .help-dot.active { background: var(--accent-blue); }

  #help-actions {
    display: flex;
    gap: 8px;
  }

  #help-back {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 11px;
    padding: 7px 14px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
  }
  #help-back:hover:not(:disabled) { border-color: #666; color: var(--text); background: var(--btn-bg); }
  #help-back:disabled { opacity: 0.3; cursor: not-allowed; }

  #help-next {
    background: rgba(74,108,247,0.15);
    border: 1px solid rgba(74,108,247,0.5);
    border-radius: 6px;
    color: #7b9fff;
    font-family: var(--font);
    font-size: 11px;
    padding: 7px 16px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
  }
  #help-next:hover { background: rgba(74,108,247,0.25); border-color: rgba(74,108,247,0.8); color: #a0b8ff; }

  /* ─── CANVAS SETTINGS MODAL ──────────────────────────────────────────────── */
  #canvas-modal {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 200;
    align-items: center;
    justify-content: center;
  }
  #canvas-modal.visible { display: flex; }

  #canvas-modal-box {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 22px 26px 24px;
    width: 340px;
    max-width: calc(100vw - 40px);
    display: flex;
    flex-direction: column;
    gap: 16px;
    box-shadow: 0 16px 48px rgba(0,0,0,0.5);
  }

  #canvas-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  #canvas-modal-title {
    font-family: var(--font);
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.1em;
    color: var(--text-dim);
  }

  #canvas-modal-close {
    width: 26px; height: 26px;
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-dim);
    font-size: 12px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
    padding: 0;
  }
  #canvas-modal-close:hover { border-color: #666; color: var(--text); background: var(--btn-bg); }

  .canvas-section-label {
    font-family: var(--font);
    font-size: 10px;
    letter-spacing: 0.1em;
    color: var(--text-dim);
    margin-bottom: -8px;
  }

  /* Aspect ratio picker */
  #ratio-options {
    display: flex;
    gap: 10px;
    align-items: flex-end;
  }

  .ratio-btn {
    flex: 1;
    background: var(--btn-bg);
    border: 1.5px solid var(--border);
    border-radius: 8px;
    padding: 12px 8px 10px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    transition: all 0.15s;
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 11px;
  }
  .ratio-btn:hover { background: var(--btn-active); border-color: #666; color: var(--text); }
  .ratio-btn.active {
    border-color: var(--accent-blue);
    background: rgba(74,108,247,0.12);
    color: #7b9fff;
  }

  /* Visual ratio preview boxes */
  .ratio-preview {
    background: var(--text-dim);
    border-radius: 2px;
    opacity: 0.5;
    transition: opacity 0.15s;
  }
  .ratio-btn.active .ratio-preview { opacity: 1; background: #7b9fff; }
  .ratio-btn:hover .ratio-preview  { opacity: 0.8; }

  .ratio-16-9 { width: 48px; height: 27px; }
  .ratio-1-1  { width: 36px; height: 36px; }
  .ratio-9-16 { width: 24px; height: 42px; }

  /* Background color row */
  #canvas-bg-row {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  #canvas-bg-wrap {
    position: relative;
    width: 36px; height: 36px;
    border-radius: 6px;
    border: 1.5px solid var(--border);
    overflow: hidden;
    cursor: pointer;
    flex-shrink: 0;
  }
  #canvas-bg-input {
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    opacity: 0; cursor: pointer;
  }
  #canvas-bg-preview {
    position: absolute; inset: 0;
    background: #ffffff;
    pointer-events: none;
    /* Checkerboard for transparency perception */
    background-image:
      linear-gradient(45deg, #555 25%, transparent 25%),
      linear-gradient(-45deg, #555 25%, transparent 25%),
      linear-gradient(45deg, transparent 75%, #555 75%),
      linear-gradient(-45deg, transparent 75%, #555 75%);
    background-size: 8px 8px;
    background-position: 0 0, 0 4px, 4px -4px, -4px 0;
  }
  #canvas-bg-preview::after {
    content: '';
    position: absolute; inset: 0;
    background: inherit;
  }

  #canvas-bg-hex {
    font-family: var(--font);
    font-size: 12px;
    color: var(--text-dim);
  }

  /* Footer buttons */
  #canvas-modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 4px;
  }

  #canvas-modal-cancel {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-dim);
    font-family: var(--font);
    font-size: 11px;
    padding: 8px 16px;
    cursor: pointer;
    transition: all 0.15s;
  }
  #canvas-modal-cancel:hover { border-color: #666; color: var(--text); background: var(--btn-bg); }

  #canvas-modal-save {
    background: rgba(74,108,247,0.15);
    border: 1px solid rgba(74,108,247,0.5);
    border-radius: 6px;
    color: #7b9fff;
    font-family: var(--font);
    font-size: 11px;
    padding: 8px 20px;
    cursor: pointer;
    transition: all 0.15s;
  }
  #canvas-modal-save:hover { background: rgba(74,108,247,0.25); border-color: rgba(74,108,247,0.8); color: #a0b8ff; }

  /* ─── MOBILE RESPONSIVE ──────────────────────────────────────────────────── */
  @media (max-width: 640px) {
    /* Toolbar scrolls horizontally rather than wrapping */
    #toolbar {
      overflow-x: auto;
      -webkit-overflow-scrolling: touch;
      gap: 4px;
      padding: 0 8px;
      scrollbar-width: none;
    }
    #toolbar::-webkit-scrollbar { display: none; }

    /* Slightly larger touch targets */
    .tool-btn,
    #color-btn,
    #canvas-settings-btn,
    #info-btn {
      width: 42px;
      height: 42px;
      flex-shrink: 0;
    }

    .mode-btn {
      height: 42px;
      padding: 0 12px;
      font-size: 10px;
    }

    #export-btn {
      height: 42px;
      padding: 0 12px;
      font-size: 10px;
      white-space: nowrap;
    }

    #toolbar { height: 58px; }

    /* Bottom panel: stack timeline below the controls row */
    #bottom-panel { padding: 10px 12px; gap: 8px; }

    .layer-tab,
    #add-layer-btn { height: 42px; }

    .ctrl-btn { width: 42px; height: 42px; padding: 0; gap: 0; }
    .rec-btn-label { display: none; }
    .ctrl-btn.rec-btn { width: 42px; padding: 0; }

    #duration-select { height: 42px; }

    /* Modals full-width on mobile */
    #canvas-modal-box,
    #help-box {
      width: calc(100vw - 24px);
      padding: 18px 18px 20px;
    }

    /* Inspector full-width */
    #inspector { width: calc(100vw - 32px); }

    /* Status text readable */
    #status { font-size: 10px; }
  }
