  :root {
    --bar: #ece9d8;
    --bar-dark: #d4d0c8;
    --ink: #1c1c1c;
    --accent: #2f6fd0;
    --good: #2f9e44;
    --warn: #c0392b;
  }
  * { box-sizing: border-box; }
  html, body {
    margin: 0;
    height: 100%;
    font-family: "Segoe UI", Tahoma, Verdana, system-ui, sans-serif;
    font-size: 13px;
    color: var(--ink);
    background: #5a7fa8;
    user-select: none;
    -webkit-user-select: none;
    overflow: hidden;
  }
  body { display: flex; flex-direction: column; }

  header {
    background: linear-gradient(#4a7fbc, #2c5f96);
    color: #fff;
    padding: 4px 8px;
    font-weight: 600;
    letter-spacing: .3px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,.4);
    flex: none;
  }
  header .logo { font-size: 15px; white-space: nowrap; }
  header .actions { margin-left: auto; display: flex; gap: 4px; flex-wrap: wrap; }

  /* the open animation: its name and whether it is safely kept */
  .project { display: flex; align-items: center; gap: 5px; }
  #projName {
    font: inherit; font-size: 13px; font-weight: 600;
    width: 150px;
    padding: 3px 6px;
    border: 1px solid rgba(255,255,255,.35);
    border-radius: 5px;
    background: rgba(255,255,255,.14);
    color: #fff;
    user-select: text; -webkit-user-select: text;
  }
  #projName:focus { outline: none; background: #fff; color: var(--ink); }
  .savechip {
    font-size: 11px; font-weight: 600;
    padding: 3px 7px;
    border-radius: 10px;
    white-space: nowrap;
    background: rgba(255,255,255,.18);
  }
  .savechip.saved  { background: rgba(80,200,120,.35); }
  .savechip.saving { background: rgba(255,215,90,.35); }
  .savechip.error, .savechip.off { background: #ffd6d1; color: var(--warn); }

  /* Tabs: only one group of controls is on screen at a time, which is what
     keeps the top bar short enough to leave the canvas some room. */
  #tabs { display: flex; gap: 3px; margin-left: 6px; flex-wrap: wrap; }
  .tab {
    font: inherit; font-size: 12px; font-weight: 600;
    background: rgba(255,255,255,.16);
    color: #fff;
    border: 1px solid rgba(255,255,255,.3);
    border-radius: 5px;
    padding: 4px 9px;
    cursor: pointer;
    white-space: nowrap;
  }
  .tab:hover { background: rgba(255,255,255,.3); }
  .tab.active {
    background: var(--bar);
    color: var(--ink);
    border-color: var(--bar);
  }

  #toolbar {
    background: var(--bar);
    border-bottom: 1px solid #a9a495;
    padding: 4px 8px;
    flex: none;
    max-height: 30vh;
    overflow-y: auto;
  }
  .panel { display: none; }
  .panel.active {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 12px;
  }
  .group { display: flex; flex-direction: column; gap: 3px; }
  .label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: .6px;
    color: #6b6759;
    white-space: nowrap;
  }
  .row { display: flex; align-items: center; gap: 3px; flex-wrap: wrap; }
  .sep { width: 1px; align-self: stretch; background: #b8b3a3; box-shadow: 1px 0 0 #fff; }
  .hidden { display: none !important; }

  button.tool, button.act {
    font: inherit;
    color: var(--ink);
    background: linear-gradient(#fdfdfd, #e6e2d6);
    border: 1px solid #a9a495;
    border-radius: 4px;
    padding: 4px 7px;
    cursor: pointer;
    line-height: 1;
    display: flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
  }
  button.tool:hover, button.act:hover { background: linear-gradient(#fff, #f2efe6); border-color: #7d7969; }
  button.tool:active, button.act:active { background: #ddd8c8; }
  button.tool.active {
    background: linear-gradient(#cfe0f7, #a9c8ee);
    border-color: var(--accent);
    box-shadow: inset 0 1px 2px rgba(0,0,0,.15);
  }
  button.tool .ico { font-size: 14px; }
  button:disabled { opacity: .4; cursor: default; }
  button.hero {
    background: linear-gradient(#ffd76a, #f0a933);
    border-color: #b07d19;
    font-weight: 600;
  }
  button.hero:hover { background: linear-gradient(#ffe28c, #f5b845); }
  /* Pulsing glow so the AI batch-animate button stands out from the rest of the toolbar */
  button.glow {
    animation: glow-pulse 1.8s ease-in-out infinite;
  }
  button.glow:hover { animation-play-state: paused; }
  @keyframes glow-pulse {
    0%, 100% { box-shadow: 0 0 2px 0 rgba(240, 169, 51, .5); }
    50% { box-shadow: 0 0 14px 4px rgba(240, 169, 51, .9); }
  }
  .mini { padding: 3px 5px; font-size: 12px; }
  /* Save shows a dot until the animation is in a file on the computer */
  #save { position: relative; }
  #save.needs::after {
    content: ''; position: absolute; top: -3px; right: -3px;
    width: 9px; height: 9px; border-radius: 50%;
    background: #ff7a1a; border: 1.5px solid #fff;
  }
  :focus-visible { outline: 2px solid #ffbf3f; outline-offset: 1px; }

  #palette { display: grid; grid-template-columns: repeat(14, 16px); grid-auto-rows: 16px; gap: 2px; }
  .swatch {
    width: 16px; height: 16px;
    border: 1px solid #7d7969;
    border-radius: 2px;
    cursor: pointer;
    padding: 0;
  }
  .swatch:hover { outline: 2px solid var(--accent); outline-offset: 1px; }

  #currentColors { position: relative; width: 42px; height: 42px; }
  #currentColors .chip {
    position: absolute; width: 26px; height: 26px; padding: 0;
    border: 1px solid #55524a; border-radius: 3px;
    box-shadow: 0 1px 2px rgba(0,0,0,.3);
  }
  #primaryChip { top: 0; left: 0; z-index: 2; }
  #secondaryChip { bottom: 0; right: 0; }
  #swapColors {
    position: absolute; top: -2px; right: -2px; z-index: 3;
    font-size: 10px; line-height: 1; padding: 1px 3px;
    background: #fff; border: 1px solid #a9a495; border-radius: 3px; cursor: pointer;
  }

  input[type=range] { width: 92px; }
  input[type=color] { width: 32px; height: 24px; padding: 0; border: 1px solid #a9a495; background: #fff; cursor: pointer; }
  input[type=number] { width: 46px; font: inherit; padding: 2px 4px; border: 1px solid #a9a495; border-radius: 3px; }
  label.check { display: flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }

  #sizePreviewWrap {
    width: 42px; height: 42px; background: #fff;
    border: 1px solid #a9a495; border-radius: 3px;
    display: flex; align-items: center; justify-content: center;
    overflow: hidden;
  }
  #sizeDot { border-radius: 50%; background: #222; }

  #wheelWrap { display: flex; align-items: center; gap: 4px; }
  #wheel {
    width: 96px; height: 96px;
    border-radius: 50%;
    cursor: crosshair;
    box-shadow: 0 1px 4px rgba(0,0,0,.35);
    background: #888;
  }
  .vslider {
    width: 84px !important;
    transform: rotate(270deg);
    margin: 0 -30px;
  }
  #hexInput {
    width: 76px; font: inherit; font-size: 11px;
    padding: 2px 4px; border: 1px solid #a9a495; border-radius: 3px;
    font-family: ui-monospace, Menlo, monospace;
    user-select: text; -webkit-user-select: text;
  }
  #kindPicker .stamp { font-size: 15px; padding: 3px 5px; }
  #efxRow .stamp { font-size: 12px; padding: 3px 6px; }
  #efxRow .stamp.on { background: #ffd76a; border-color: #b07d19; font-weight: 600; }

  #stamps   { display: flex; gap: 3px; flex-wrap: wrap; max-width: 430px; }
  #poses    { display: flex; gap: 3px; flex-wrap: wrap; max-width: 460px; }
  #fxButtons{ display: flex; gap: 3px; flex-wrap: wrap; max-width: 620px; }
  #brushTools, #shapeTools { max-width: 100%; }
  .stamp {
    font: inherit;
    font-size: 16px; line-height: 1;
    color: var(--ink);
    background: #fff; border: 1px solid #a9a495; border-radius: 4px;
    padding: 2px 4px; cursor: pointer;
  }
  .stamp:hover { background: #e8f0fd; border-color: var(--accent); }
  .stamp.active { background: #a9c8ee; border-color: var(--accent); }
  .pose-btn {
    font: inherit; font-size: 11px;
    color: var(--ink);
    background: linear-gradient(#fdfdfd, #e6e2d6);
    border: 1px solid #a9a495; border-radius: 4px;
    padding: 3px 6px; cursor: pointer;
    white-space: nowrap;
  }
  .pose-btn:hover { background: #e8f0fd; border-color: var(--accent); }
  .pose-btn.active { background: #a9c8ee; border-color: var(--accent); font-weight: 600; }

  /* ------------------------------------------------------------ library */
  .lib { display: flex; flex-direction: column; gap: 3px; min-width: 0; flex: 1 1 520px; }
  .lib-grid {
    display: flex; flex-wrap: wrap; gap: 4px;
    max-height: 104px; overflow-y: auto;
    padding: 2px;
  }
  .lib-item {
    position: relative;
    width: 46px; height: 46px;
    padding: 3px;
    background: #fff;
    border: 1px solid #a9a495; border-radius: 6px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
  }
  .lib-item:hover { border-color: var(--accent); background: #e8f0fd; }
  .lib-item img, .lib-item canvas { max-width: 100%; max-height: 100%; display: block; pointer-events: none; }
  .lib-item.wide { width: 74px; }
  .lib-item.on { outline: 3px solid var(--accent); outline-offset: 0; }
  .lib-item .x {
    position: absolute; top: -5px; right: -5px;
    width: 16px; height: 16px; line-height: 14px; padding: 0;
    font-size: 10px; border-radius: 50%;
    border: 1px solid #a9a495; background: #fff; cursor: pointer;
  }
  .lib-item .tag {
    position: absolute; left: 1px; bottom: 1px; right: 1px;
    font-size: 9px; line-height: 1.1; text-align: center;
    color: #3a382f; background: rgba(255,255,255,.85);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    pointer-events: none;
  }
  .snd {
    font: inherit; font-size: 12px;
    color: var(--ink);
    background: #fff; border: 1px solid #a9a495; border-radius: 14px;
    padding: 4px 9px; cursor: pointer; white-space: nowrap;
  }
  .snd:hover { border-color: var(--accent); background: #e8f0fd; }
  .snd.on { background: #a9c8ee; border-color: var(--accent); font-weight: 600; }
  .lib-side { display: flex; flex-direction: column; gap: 4px; max-width: 360px; }
  .lib-note { font-size: 11px; color: #5d594c; max-width: 340px; }

  #stage {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    padding: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  /* The picture is always 1000x640 internally; the box just scales to whatever
     room is left over, so the canvas is as big as the window allows. */
  #canvasWrap {
    position: relative;
    width: 100%;
    max-width: 1000px;
    max-height: 100%;
    aspect-ratio: 1000 / 640;
    box-shadow: 0 4px 18px rgba(0,0,0,.45);
    background: #fff;
    border: 1px solid #3c3c3c;
  }
  /* Layer order: background scene, onion skin, painted bitmap, pictures +
     puppets + effects, live handles. */
  #canvasWrap canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
  }
  #bgc     { z-index: 0; pointer-events: none; }
  #onion   { z-index: 1; opacity: .28; pointer-events: none; }
  #paint   { z-index: 2; }
  #anim    { z-index: 3; pointer-events: none; }
  #overlay { z-index: 4; cursor: crosshair; }

  #textInput {
    position: absolute; z-index: 5;
    background: rgba(255,255,255,.85);
    border: 1px dashed var(--accent);
    outline: none;
    font-family: "Comic Sans MS", "Segoe UI", sans-serif;
    padding: 0 2px;
    min-width: 40px;
    user-select: text; -webkit-user-select: text;
  }
  #textInput.hidden { display: none; }

  /* ------------------------------------------------------ timeline panel */
  #timeline {
    flex: none;
    background: var(--bar);
    border-top: 1px solid #a9a495;
    padding: 4px 8px;
    display: flex;
    flex-direction: column;
    gap: 3px;
  }
  #transport { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
  #transport .fps { display: flex; align-items: center; gap: 4px; }

  #strip {
    display: flex;
    gap: 5px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 2px 2px 4px;
    min-height: 58px;
    align-items: flex-start;
  }
  .frameCell {
    flex: none;
    position: relative;
    border: 2px solid #a9a495;
    border-radius: 4px;
    background: #fff;
    padding: 0;
    cursor: pointer;
    line-height: 0;
    box-shadow: 0 1px 2px rgba(0,0,0,.2);
  }
  .frameCell canvas { display: block; width: 70px; height: 45px; border-radius: 2px; }
  .frameCell .num {
    position: absolute; left: 0; bottom: 0;
    background: rgba(0,0,0,.6); color: #fff;
    font-size: 10px; line-height: 1;
    padding: 2px 4px; border-radius: 0 4px 0 3px;
  }
  .frameCell .badge {
    position: absolute; right: 2px; top: 2px;
    font-size: 10px; line-height: 1;
  }
  .frameCell.current { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(47,111,208,.35); }
  .frameCell.playhead { border-color: #d33; }

  footer {
    background: var(--bar-dark);
    border-top: 1px solid #a9a495;
    padding: 2px 10px;
    font-size: 12px;
    display: flex;
    gap: 16px;
    color: #3a382f;
    flex: none;
    white-space: nowrap;
    overflow: hidden;
  }
  footer .spacer { margin-left: auto; }
  footer a { color: #2c5f96; }

  body.playing #overlay { cursor: default; }

  /* -------------------------------------------------------------- modals */
  .modal {
    position: fixed; inset: 0; z-index: 50;
    background: rgba(20,30,50,.55);
    display: flex; align-items: center; justify-content: center;
    padding: 16px;
  }
  .modal-card {
    background: var(--bar);
    border: 1px solid #7d7969;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0,0,0,.45);
    padding: 14px 16px;
    max-width: 100%;
    max-height: 100%;
    overflow: auto;
  }
  .modal-card h2 { margin: 0 0 8px; font-size: 17px; }
  .modal-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; margin-top: 10px; }
  .ask { width: 460px; }
  .ask p { margin: 6px 0 10px; font-size: 14px; line-height: 1.4; user-select: text; -webkit-user-select: text; }
  .ask-buttons { display: flex; flex-direction: column; gap: 6px; }
  /* A long answer wraps inside its button; it must never push the whole sheet sideways. */
  .ask-buttons button { white-space: normal; text-align: center; line-height: 1.25; min-width: 0; max-width: 100%; }
  .ask-buttons button { font-size: 14px; padding: 9px 12px; justify-content: flex-start; }

  .animate { width: 560px; }
  .animate .gal-note { margin: 4px 0 10px; }
  #animatePrompt {
    width: 100%; font: inherit; font-size: 14px; padding: 6px 8px; margin-top: 6px;
    border: 1px solid #a9a495; border-radius: 5px; resize: vertical; box-sizing: border-box;
  }
  #animatePreviewWrap canvas {
    width: 100%; aspect-ratio: 1000 / 640; background: #e9e5d7;
    border: 1px solid #a9a495; border-radius: 6px; touch-action: none; cursor: grab;
  }
  #animatePreviewWrap .row { margin-top: 6px; }
  #animatePreviewWrap input[type=range] { flex: 1; width: auto; }
  #askPreview:empty { display: none; }
  #askPreview { display: flex; justify-content: center; margin-bottom: 6px; }
  #askPreview canvas, #askPreview img {
    max-width: 100%; max-height: 180px; border: 1px solid #a9a495; border-radius: 4px;
    background: repeating-conic-gradient(#e6e6e6 0 25%, #fff 0 50%) 0 0 / 16px 16px;
  }

  .gallery { width: 860px; }
  .gal-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
  .gal-head h2 { margin: 0 auto 0 0; }
  .gal-note { font-size: 12px; color: #5d594c; margin: 6px 0 10px; }
  .gal-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
  .gal-card {
    background: #fff;
    border: 2px solid #c9c4b4; border-radius: 8px;
    overflow: hidden;
    display: flex; flex-direction: column;
  }
  .gal-card.current { border-color: var(--accent); }
  .gal-open {
    font: inherit; color: var(--ink);
    border: 0; background: none; padding: 0; cursor: pointer; text-align: left;
    display: flex; flex-direction: column;
  }
  .gal-open img { width: 100%; aspect-ratio: 1000 / 640; object-fit: cover; background: #f4f4f4; display: block; }
  .gal-name { font-weight: 600; padding: 5px 7px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .gal-meta { font-size: 11px; color: #6b6759; padding: 0 7px 4px; }
  .gal-actions { display: flex; gap: 3px; padding: 0 6px 6px; flex-wrap: wrap; }
  .gal-actions button { font-size: 12px; padding: 3px 6px; }
  .gal-empty { color: #5d594c; padding: 18px; text-align: center; grid-column: 1 / -1; }
  #galTrash { margin-top: 12px; }
  #galTrash summary { cursor: pointer; font-weight: 600; margin-bottom: 8px; }

  #toast {
    position: fixed; left: 50%; top: 74px; transform: translateX(-50%);
    z-index: 70;
    background: #222c3a; color: #fff;
    padding: 9px 14px; border-radius: 8px;
    font-size: 14px; max-width: min(560px, calc(100vw - 32px));
    box-shadow: 0 6px 24px rgba(0,0,0,.35);
    pointer-events: none;
  }
  #busy {
    position: fixed; inset: 0; z-index: 80;
    background: rgba(20,30,50,.45);
    display: flex; align-items: center; justify-content: center;
  }
  .busy-card {
    background: #fff; border-radius: 10px; padding: 14px 18px;
    display: flex; align-items: center; gap: 10px; font-size: 14px;
    box-shadow: 0 8px 30px rgba(0,0,0,.35);
  }
  .spinner {
    width: 18px; height: 18px; border-radius: 50%;
    border: 3px solid #c9d8f2; border-top-color: var(--accent);
    animation: spin .8s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }
  #dropHint {
    position: fixed; inset: 10px; z-index: 60;
    border: 4px dashed #ffd76a; border-radius: 16px;
    background: rgba(20,30,50,.55); color: #fff;
    font-size: 26px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    pointer-events: none;
  }

  /* ------------------------------------------------------ picture editor */
  .editor { width: 1080px; display: flex; flex-direction: column; }
  .editor.busy, .modal.busy .editor { cursor: progress; }
  .ed-body { display: flex; gap: 14px; min-height: 0; flex-wrap: wrap; }
  .ed-left { flex: 1 1 520px; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
  .ed-right { flex: 0 1 380px; display: flex; flex-direction: column; gap: 10px; }
  .ed-stage {
    height: min(52vh, 440px);
    display: flex; align-items: center; justify-content: center;
    background: repeating-conic-gradient(#dcdcdc 0 25%, #fff 0 50%) 0 0 / 18px 18px;
    border: 1px solid #a9a495; border-radius: 6px;
    overflow: hidden;
  }
  #edView { cursor: crosshair; touch-action: none; }
  .ed-acts { flex-wrap: wrap; gap: 4px; }
  .ed-acts .act.on { background: #ffe9b8; border-color: #c98a00; }
  #edPieceSize { width: 110px; }
  .ed-opt .act:disabled { opacity: .45; }
  .ed-hint { min-width: 160px; }
  .ed-under { min-height: 28px; }
  .ed-opt { display: flex; align-items: center; gap: 4px; }
  .ed-hint { font-size: 12px; color: #5d594c; margin-left: auto; }
  .ed-group { display: flex; flex-direction: column; gap: 4px; }
  .ed-group.row { flex-direction: row; }
  .ed-fx { display: flex; flex-wrap: wrap; gap: 4px; }
  .ed-fx .stamp { font-size: 12px; padding: 4px 6px; }
  .ed-group input[type=text] {
    font: inherit; font-size: 14px; padding: 5px 7px;
    border: 1px solid #a9a495; border-radius: 4px;
    user-select: text; -webkit-user-select: text;
  }

  @media (max-width: 760px) {
    #projName { width: 110px; }
    header .logo { font-size: 13px; }
  }
