/* Donuts.
 *
 * Dark chrome, light chart — the drawing is the brightest thing on the screen, same
 * as the rest of the house. The layout rules are the house rules: one viewport tall,
 * nothing scrolls except the two rails that are meant to, and on a phone the controls
 * are a band *below* the chart taking at most half the screen, so the chart stays
 * visible while you tune it (a panel over the artwork hides the thing you are making).
 */

:root {
  --bg: #141726;
  --panel: #1b1f31;
  --panel-2: #212639;
  --line: #2c3350;
  --line-soft: #242a42;
  --ink: #e8ecf6;
  --ink-dim: #9aa3bd;
  --accent: #d8b48c;
  --good: #7ef0d0;
  --card: #f7f4ef;
  --topbar-h: 52px;
  --rail-w: 356px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body.donuts {
  margin: 0;
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr;
  height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ------------------------------------------------------------------ top bar -- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 7px; font-weight: 600; letter-spacing: .01em; }
.brand-mark { color: var(--accent); font-size: 17px; }
.home-btn, .ghost {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 10px;
  font-size: 13px;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.home-btn:hover, .ghost:hover { border-color: var(--accent); }
.ghost.small { padding: 4px 8px; font-size: 12px; }
.ghost:disabled { opacity: .4; cursor: default; }
.topbar-actions { margin-left: auto; display: flex; gap: 6px; }

.modes { display: flex; gap: 4px; margin-left: 6px; padding: 3px; background: var(--bg); border-radius: 10px; }
.mode-tab {
  padding: 6px 13px;
  font-size: 13px;
  color: var(--ink-dim);
  background: transparent;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.mode-tab.is-active { color: var(--ink); background: var(--panel-2); box-shadow: inset 0 0 0 1px var(--line); }

/* -------------------------------------------------------------------- shell -- */
.shell {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  grid-template-areas: "rail stage";
  /* The grip sits in the seam: it is how the sidebar is resized, and it must not take
     layout width away from either side. */
  position: relative;
  min-height: 0;
}
.rail {
  grid-area: rail;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.stage {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 14px;
}
.pane { display: flex; flex-direction: column; gap: 9px; }
.pane[hidden] { display: none; }
.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--ink-dim);
}
.row-label { font-size: 12px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-dim); }
.hint { margin: 0; font-size: 11.5px; line-height: 1.45; color: var(--ink-dim); }
.notice { margin: 0; font-size: 12.5px; color: var(--ink-dim); }
.notice.is-bad { color: #ffb4ab; }
.notice.is-good { color: var(--good); }

.field { display: flex; flex-direction: column; gap: 5px; }
.field input[type="text"] {
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
}
.field input[type="text"]:focus { outline: none; border-color: var(--accent); }
.field output { color: var(--ink); font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

/* -------------------------------------------------------------------- chart -- */
.chart-card {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 8px;
  background: var(--card);
  border-radius: 18px;
  overflow: hidden;
}
/* The host is taken out of flow so its own size can never be driven by the drawing
   inside it. Sized by the svg, it measured as tall as it was wide, the chart overflowed
   the card and the next render measured the overflow — a loop that clipped the edges. */
.chart { position: absolute; inset: 8px; display: grid; place-items: center; }
.chart svg { display: block; width: 100%; height: 100%; }
.chart .seg { cursor: pointer; transition: opacity .12s ease-out; }
.chart-card.is-focusing .seg { opacity: .18; }
.chart-card.is-focusing .seg.is-lit { opacity: 1; }

.tip {
  position: absolute;
  z-index: 5;
  max-width: 240px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.4;
  color: var(--ink);
  background: rgba(20, 23, 38, .94);
  border: 1px solid var(--line);
  border-radius: 10px;
  pointer-events: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .35);
}
.tip b { color: #fff; font-weight: 600; }
.tip .tip-dim { color: var(--ink-dim); }
.tip[hidden] { display: none; }

.footnote {
  margin: 0;
  padding: 8px 11px;
  font-size: 12px;
  color: var(--ink-dim);
  background: var(--panel);
  border-left: 2px solid var(--accent);
  border-radius: 0 10px 10px 0;
}
.footnote[hidden] { display: none; }

/* ------------------------------------------------------------- the drop zone -- */
.drop {
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 16px 14px;
  text-align: center;
  background: var(--panel-2);
  border: 1.5px dashed var(--line);
  border-radius: 14px;
}
.drop.is-over { border-color: var(--accent); background: rgba(216, 180, 140, .09); }
.drop-lead { margin: 0; font-weight: 600; }
.drop-hint { margin: 0; font-size: 12px; line-height: 1.5; color: var(--ink-dim); }
.drop-hint b { color: var(--ink); font-weight: 600; }
.drop-actions { display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; }
.btn {
  padding: 8px 14px;
  font-size: 13px;
  color: #1a1520;
  background: var(--accent);
  border: 0;
  border-radius: 9px;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.06); }
.link { font-size: 12.5px; }
.is-source {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 10px;
  font-size: 12px;
  color: var(--ink-dim);
  background: var(--panel-2);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
}

/* --------------------------------------------------------------------- tree -- */
.tree { display: flex; flex-direction: column; gap: 2px; }
.tree.empty { color: var(--ink-dim); font-size: 12.5px; }
.node {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 2px 4px;
  border-radius: 8px;
  min-width: 0;
}
.node:hover { background: rgba(255, 255, 255, .03); }
.node.is-active { background: rgba(216, 180, 140, .15); box-shadow: inset 0 0 0 1px rgba(216, 180, 140, .55); }
.node .depth {
  flex: none;
  width: 20px;
  text-align: center;
  font-size: 9.5px;
  letter-spacing: .04em;
  color: var(--ink-dim);
}
.node input {
  min-width: 0;
  padding: 4px 6px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
}
.node input:hover { border-color: var(--line-soft); }
.node input:focus { outline: none; background: var(--bg); border-color: var(--accent); }
.node input.name { flex: 1; font-size: 13px; }
.node input.value {
  flex: none;
  width: 78px;
  text-align: right;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
}
.node .sum {
  flex: none;
  width: 78px;
  text-align: right;
  font-size: 12.5px;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
}
.node button.icon {
  flex: none;
  width: 24px;
  height: 24px;
  font-size: 13px;
  line-height: 1;
  color: var(--ink-dim);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
  cursor: pointer;
}
.node button.icon:hover { color: var(--ink); border-color: var(--line); }
.node button.icon.del:hover { color: #ffb4ab; border-color: #6d3a3a; }
.node .swatch { flex: none; width: 9px; height: 18px; border-radius: 3px; margin-right: 2px; }

/* ----------------------------------------------------------------- palettes -- */
.palettes { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 6px; }
.palette {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
}
.palette:hover { border-color: var(--accent); }
.palette.is-active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.palette .ramp { display: flex; height: 22px; border-radius: 5px; overflow: hidden; }
/* One chip per colour family, drawn as a gradient from its darkest to its lightest
   shade: a palette is families and shades, and four flat chips showed neither. */
.palette .ramp .fam { flex: 1; }
.palette .meta { display: flex; align-items: center; gap: 5px; min-width: 0; }
.palette .label { font-size: 11px; color: var(--ink-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette.is-active .label { color: var(--ink); }
.palette .badge {
  flex: none;
  padding: 1px 5px;
  font-size: 9px;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: #cdd6e6;
  background: #2b3352;
  border: 1px solid #3c4570;
  border-radius: 20px;
}

/* ----------------------------------------------------------- background and texture -- */
.bgs { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
.bg {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 7px 8px;
  font: inherit;
  font-size: 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
}
.bg:hover { border-color: var(--accent); }
.bg.is-active { box-shadow: 0 0 0 2px var(--accent) inset; }
.bg .pip { flex: none; width: 11px; height: 11px; border: 1.5px solid currentColor; border-radius: 50%; }
.bg .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.textures { display: flex; flex-wrap: wrap; gap: 6px; }
.texture {
  padding: 6px 9px;
  font: inherit;
  font-size: 11.5px;
  color: var(--ink);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.texture:hover { border-color: var(--accent); }
.texture.is-active { border-color: var(--accent); color: var(--accent); }

select {
  width: 100%;
  padding: 7px 9px;
  font: inherit;
  font-size: 12.5px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
}
select:focus { outline: none; border-color: var(--accent); }

.head-actions { display: flex; align-items: center; gap: 5px; }

/* ------------------------------------------------------------------- the twisty -- */
.node .twisty {
  flex: none;
  width: 18px;
  height: 22px;
  padding: 0;
  font-size: 10px;
  line-height: 1;
  color: var(--ink-dim);
  background: transparent;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
.node .twisty:hover { color: var(--ink); background: rgba(255, 255, 255, .06); }
.node .twisty.is-leaf { cursor: default; background: none; }

/* ------------------------------------------------------------------ the rail grip -- */
.rail-grip {
  /* Out of the grid flow: a third grid item would push the stage one column over. */
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--rail-w);
  width: 9px;
  margin-left: -4px;
  cursor: col-resize;
  background: transparent;
  z-index: 3;
  touch-action: none;
}
.rail-grip::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 46px;
  transform: translate(-50%, -50%);
  background: var(--line);
  border-radius: 2px;
}
.rail-grip:hover::after, .rail-grip:focus-visible::after, .rail-grip.is-dragging::after {
  background: var(--accent);
  height: 84px;
}
.rail-grip:focus-visible { outline: none; }
body.is-resizing { cursor: col-resize; user-select: none; }
body.is-resizing iframe { pointer-events: none; }

/* -------------------------------------------------------------------- phone -- */
/* ------------------------------------------------------------ the examples -- */
.demos { margin-top: 12px; }
.demo-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.demo {
  padding: 7px 10px;
  font: inherit;
  font-size: 12px;
  color: var(--ink);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
}
.demo:hover { border-color: var(--accent); color: var(--accent); }

/* ------------------------------------------------------- text in the centre -- */
textarea#centre-text {
  width: 100%;
  min-height: 50px;
  padding: 7px 9px;
  font: inherit;
  font-size: 13px;
  line-height: 1.35;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  resize: vertical;
}
textarea#centre-text:focus { outline: none; border-color: var(--accent); }
.row-sub { color: var(--ink-dim); font-size: 11px; font-weight: 400; }

/* ---------------------------------------------------- the small-category rule -- */
.inline-field {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--ink-dim);
}
.inline-field input[type=number] {
  width: 62px;
  padding: 5px 7px;
  font: inherit;
  font-size: 12px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  text-align: right;
}

/* --------------------------------------------------------------- the editor -- */
.node input.total {
  width: 66px;
  padding: 3px 5px;
  font-size: 12px;
  text-align: right;
  color: var(--ink);
  background: transparent;
  border: 1px dashed var(--line);
  border-radius: 6px;
}
.node input.total:focus { background: var(--bg); border-style: solid; }
.node input.total.is-low { border-color: #b4553f; color: #d98a72; }

/* ------------------------------------------------------------ hover: a bucket -- */
.tip-head { color: var(--ink-dim); font-size: 11px; }
.tip-member { display: block; padding-left: 8px; }
.tip-member b { font-weight: 600; }

@media (max-width: 900px) {
  .shell {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
    grid-template-areas: "stage" "rail";
  }
  /* The band below the chart: at most half the screen, scrolling inside itself, so
     the chart keeps the rest and stays wholly visible. */
  .rail {
    max-height: 50dvh;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    border-right: none;
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
  }
  .stage { padding: 10px; gap: 8px; }
  /* A square, wholly given over to the chart: whichever of width or the leftover
     height is smaller, and never a letterbox squeezed by the controls below. */
  .chart-card {
    flex: none;
    aspect-ratio: 1 / 1;
    width: min(100%, calc(50dvh - var(--topbar-h) - 26px));
    margin: 0 auto;
  }
  .rail-grip { display: none; }
  .head-actions { flex-wrap: wrap; }
  .palettes { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .brand-text { display: none; }
  /* Only the word "Home" goes on small screens: PNG and SVG must stay distinguishable,
     or the two export buttons become the same arrow twice. */
  .home-btn .btn-text { display: none; }
  .topbar { gap: 6px; padding: 0 9px; }
  .home-btn, .ghost { padding: 7px 9px; }
  .mode-tab { padding: 6px 10px; }
  .node input.value, .node .sum { width: 62px; }
  .node input.total { width: 56px; }
}
