:root {
  --bg: #f4f5f7;
  --card-bg: #ffffff;
  --text: #23262b;
  --muted: #6b7280;
  --border: #e2e4e9;
  --accent: #3b5bfd;
  --accent-text: #ffffff;
  --danger: #d7263d;
  --gold: #b8860b;
  --shadow: 0 1px 2px rgba(0,0,0,0.05), 0 1px 8px rgba(0,0,0,0.04);

  --die-d4: #e2703a;
  --die-d6: #3b7dc4;
  --die-d8: #8b5cf0;
  --die-d10: #159a83;
  --die-d12: #d9497c;
  --die-d20: #c99a1e;
  --die-d100: #4c5fd5;
  --die-custom: #7a7f8c;
  --die-size: 62px;

  --font-body: "Hiragino Kaku Gothic ProN", "Yu Gothic", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-heading: var(--font-body);
  --radius-card: 12px;
  --radius-pill: 999px;
  --letter-spacing-heading: normal;
  --layout-max-width: 520px;
}

[data-theme="dark"] {
  --bg: #16181d;
  --card-bg: #1f2229;
  --text: #e9eaee;
  --muted: #9aa0ab;
  --border: #2c303a;
  --accent: #5c7cfa;
  --accent-text: #0e0f12;
  --danger: #ff6b81;
  --gold: #e0b23a;
  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 1px 8px rgba(0,0,0,0.25);

  --die-d4: #ff9159;
  --die-d6: #6fa8e0;
  --die-d8: #b28bff;
  --die-d10: #33c9ad;
  --die-d12: #ff7fa8;
  --die-d20: #e8c34c;
  --die-d100: #8b98f5;
  --die-custom: #a3a8b5;
}

/* ---- skins: self-contained visual packages. A skin's defining trait is
   ARRANGEMENT - which section sits where, how sections are grouped and
   navigated - not just recolored chrome. Each skin still carries a light
   identity of its own (so it doesn't look unstyled), but that identity is
   secondary to its layout. Only "simple" (the default, no data-skin) follows
   the light/dark theme toggle above; the others commit to one fixed look. ---- */

html[data-skin="tabletop"] {
  --bg: #e9dcbd;
  --card-bg: #f8f0da;
  --text: #3b2a1a;
  --muted: #7a6647;
  --border: #cbb583;
  --accent: #8a5a1e;
  --accent-text: #fbf3e2;
  --shadow: 0 1px 2px rgba(59,42,26,0.12), 0 2px 10px rgba(59,42,26,0.1);
  --font-body: "Hiragino Mincho ProN", "Yu Mincho", Georgia, serif;
  --font-heading: "Hiragino Mincho ProN", "Yu Mincho", Georgia, serif;
  --radius-card: 6px;
  --radius-pill: 6px;
  --layout-max-width: 640px;
}

html[data-skin="office"] {
  --bg: #dfe3e8;
  --card-bg: #f4f6f8;
  --text: #1e2a33;
  --muted: #56646e;
  --border: #8ea0ac;
  --accent: #2c5f8a;
  --accent-text: #ffffff;
  --shadow: none;
  --font-body: "Hiragino Kaku Gothic ProN", "MS PGothic", sans-serif;
  --font-heading: var(--font-body);
  --radius-card: 0px;
  --radius-pill: 3px;
  --layout-max-width: 780px;
}

html[data-skin="editor"] {
  --bg: #eef0f3;
  --card-bg: #ffffff;
  --text: #24272b;
  --muted: #767c85;
  --border: #d8dce1;
  --accent: #3d4b5c;
  --accent-text: #ffffff;
  --shadow: 0 1px 2px rgba(0,0,0,0.06);
  --font-body: -apple-system, "Hiragino Sans", BlinkMacSystemFont, sans-serif;
  --font-heading: var(--font-body);
  --radius-card: 4px;
  --radius-pill: 4px;
  --layout-max-width: 860px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background 0.15s ease, color 0.15s ease;
}

.site-header {
  position: relative;
  text-align: center;
  padding: 2.5rem 1rem 1rem;
}

.gear-btn-visible {
  position: absolute;
  top: 1rem;
  right: 1rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  border-radius: var(--radius-pill);
  padding: 0.45rem 0.9rem;
  font-size: 0.85rem;
  font-family: var(--font-body);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.1s ease, background 0.15s ease;
}

.gear-btn-visible:hover {
  background: var(--accent);
  color: var(--accent-text);
}

.gear-btn-visible:active {
  transform: scale(0.96);
}

@media (max-width: 480px) {
  .gear-btn-visible {
    position: static;
    margin: 0 auto 0.9rem;
  }
}

.site-header h1 {
  margin: 0 0 0.4rem;
  font-size: 1.6rem;
  font-weight: 700;
  font-family: var(--font-heading);
  letter-spacing: var(--letter-spacing-heading);
}

.tagline {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

main.layout {
  flex: 1;
  width: 100%;
  max-width: var(--layout-max-width);
  margin: 0 auto;
  padding: 0 1rem 2rem;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas:
    "dice"
    "roll"
    "add"
    "presets"
    "history";
  gap: 1rem;
  align-items: start;
}

.layout .card {
  margin-bottom: 0;
}

.layout .roll-area {
  margin: 0;
}

.area-dice { grid-area: dice; }
.area-roll { grid-area: roll; }
.area-add { grid-area: add; }
.area-presets { grid-area: presets; }
.area-history { grid-area: history; }

/* Deliberately outside main.layout's grid, and deliberately NOT styled as a
   .card - it's a quiet footnote below the tool, not another card of equal
   weight in the column stack. A thin rule + muted, small type read as
   boilerplate near the footer rather than a fifth feature card. */
.about-section {
  width: 100%;
  max-width: var(--layout-max-width);
  margin: 0 auto;
  padding: 0 1rem;
}

.about-section section {
  border-top: 1px solid var(--border);
  padding: 1rem 0 1.5rem;
}

.about-section h2 {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0 0 0.5rem;
}

.about-section p {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.6;
  margin: 0;
}

.quick-nav {
  display: none;
}

/* ---- tabletop: add + presets share a row, like two trays laid side by
   side on a table, instead of stacking the full width of the page. ---- */
html[data-skin="tabletop"] main.layout {
  grid-template-columns: 1fr 1fr;
  grid-template-areas:
    "dice    dice"
    "roll    roll"
    "add     presets"
    "history history";
}

/* ---- office: a persistent jump-list down the left, boxy banner-style
   section headers, and a chevron connecting each step to the next - the
   layout of an internal line-of-business tool rather than an app screen. ---- */
html[data-skin="office"] main.layout {
  grid-template-columns: 150px 1fr;
  grid-template-areas:
    "nav dice"
    "nav roll"
    "nav add"
    "nav presets"
    "nav history";
  gap: 0.75rem 1.25rem;
}

html[data-skin="office"] .quick-nav {
  grid-area: nav;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: sticky;
  top: 1rem;
  align-self: start;
  border: 1px solid var(--border);
  background: var(--card-bg);
}

html[data-skin="office"] .quick-nav a {
  display: block;
  padding: 0.5rem 0.6rem;
  font-size: 0.78rem;
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
}

html[data-skin="office"] .quick-nav a:last-child {
  border-bottom: none;
}

html[data-skin="office"] .quick-nav a:hover {
  background: var(--bg);
}

html[data-skin="office"] .card {
  border: 1px solid var(--border);
  position: relative;
}

html[data-skin="office"] .card h2 {
  background: var(--accent);
  color: var(--accent-text);
  margin: -1.25rem -1.25rem 1rem;
  padding: 0.55rem 1.25rem;
  font-size: 0.88rem;
}

html[data-skin="office"] .card h2 .link-btn {
  color: var(--accent-text);
  text-decoration: underline;
}

html[data-skin="office"] .area-dice::after,
html[data-skin="office"] .area-add::after,
html[data-skin="office"] .area-presets::after {
  content: "▼";
  display: block;
  text-align: center;
  color: var(--muted);
  font-size: 0.75rem;
  margin-top: 0.6rem;
}

html[data-skin="office"] .roll-btn {
  border-radius: var(--radius-pill);
}

/* ---- editor: a narrow control rail (add + presets) beside a wide working
   canvas (current dice + roll), the way a maker tool separates its option
   panel from the thing it is building - rather than one linear scroll. ---- */
html[data-skin="editor"] main.layout {
  grid-template-columns: 260px 1fr;
  grid-template-areas:
    "add     dice"
    "presets dice"
    "presets roll"
    "history history";
  gap: 1rem;
}

html[data-skin="editor"] .area-add,
html[data-skin="editor"] .area-presets {
  background: var(--bg);
}

html[data-skin="editor"] .area-dice {
  min-height: 320px;
}

html[data-skin="editor"] .roll-area {
  margin: 0;
  justify-content: stretch;
}

html[data-skin="editor"] .roll-btn {
  width: 100%;
  padding: 0.75rem 1rem;
}

@media (max-width: 720px) {
  html[data-skin="office"] main.layout,
  html[data-skin="editor"] main.layout,
  html[data-skin="tabletop"] main.layout {
    grid-template-columns: 1fr;
    grid-template-areas:
      "dice"
      "roll"
      "add"
      "presets"
      "history";
  }
  html[data-skin="office"] .quick-nav {
    /* On a single scrolling column the jump-list earns its keep only when a
       tall sidebar would otherwise be out of reach - not here, so drop it
       rather than reflow it into a second, redundant nav bar. */
    display: none;
  }
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.card h2 {
  margin: 0 0 0.9rem;
  font-size: 1rem;
  font-weight: 600;
  font-family: var(--font-heading);
  letter-spacing: var(--letter-spacing-heading);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.preset-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.9rem;
}

.preset-chip {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-pill);
  padding: 0.35rem 0.6rem 0.35rem 0.9rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.preset-chip:hover {
  border-color: var(--accent);
}

.preset-chip.active {
  border-color: var(--accent);
  font-weight: 700;
}

.preset-chip .preset-del {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0 0.15rem;
  line-height: 1;
}

.preset-chip .preset-del:hover {
  color: var(--danger);
}

.preset-empty {
  color: var(--muted);
  font-size: 0.85rem;
}

.preset-save-row {
  display: flex;
  gap: 0.5rem;
}

.preset-save-row input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.6rem;
  background: var(--card-bg);
  color: var(--text);
}

.preset-save-row button {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  white-space: nowrap;
}

.dice-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.9rem;
}

.die.add-die-btn {
  width: 52px;
  height: 52px;
  border: none;
  padding: 0;
  font: inherit;
  opacity: 0.92;
}

.die.add-die-btn .die-number {
  font-size: 1.05rem;
  text-shadow: none;
}

.die.add-die-btn[data-sides="100"] .die-number {
  font-size: 0.9rem;
}

.die.add-die-btn:hover { opacity: 1; }
.die.add-die-btn:active { transform: scale(0.94); }

.custom-die {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.custom-die input {
  width: 90px;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card-bg);
  color: var(--text);
}

.custom-die button {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0;
}

.section-hint {
  margin: -0.5rem 0 0.9rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.dice-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  min-height: 66px;
}

.dice-grid-empty {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0;
}

.die {
  --die-color: var(--die-d6);
  width: var(--die-size, 62px);
  height: var(--die-size, 62px);
  flex-shrink: 0;
  background: var(--die-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  box-shadow: 0 2px 5px rgba(0,0,0,0.18);
  transition: transform 0.08s ease, box-shadow 0.15s ease;
  border: none;
  padding: 0;
}

.die:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(0,0,0,0.22);
}

.die:active {
  transform: scale(0.94);
}

.die-shape-d6 { border-radius: 14%; }
.die-shape-d4 { clip-path: polygon(50% 4%, 96% 92%, 4% 92%); }
.die-shape-d8 { clip-path: polygon(50% 0%, 78% 50%, 50% 100%, 22% 50%); }
.die-shape-d10 { clip-path: polygon(50% 0%, 87% 36%, 50% 100%, 13% 36%); }
.die-shape-d12 { clip-path: polygon(50% 2%, 95% 38%, 78% 96%, 22% 96%, 5% 38%); }
.die-shape-d20 { clip-path: polygon(50% 2%, 93% 25%, 93% 75%, 50% 98%, 7% 75%, 7% 25%); }
.die-shape-d100 { border-radius: 50%; }
.die-shape-custom { border-radius: 22%; }

.die-shape-custom::after {
  content: "d" attr(data-sides);
  position: absolute;
  bottom: 3px;
  right: 6px;
  font-size: 0.55rem;
  font-weight: 700;
  color: rgba(255,255,255,0.85);
}

.die-inner {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.die-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: rgba(255,255,255,0.85);
}

.die-number {
  font-size: 1.35rem;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.25);
}

.die.min-roll {
  outline: 3px solid var(--danger);
  outline-offset: 2px;
}

.die.max-roll {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

.pip-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 72%;
  height: 72%;
}

.pip {
  width: 66%;
  height: 66%;
  margin: auto;
  border-radius: 50%;
  background: transparent;
}

.pip.on {
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.4);
}

.roll-area {
  display: flex;
  justify-content: center;
  margin: 1.5rem 0;
}

.roll-btn {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: var(--radius-pill);
  padding: 0.9rem 3rem;
  font-size: 1.1rem;
  font-weight: 700;
  font-family: var(--font-heading);
  letter-spacing: var(--letter-spacing-heading);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.1s ease, opacity 0.15s ease;
}

.roll-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.roll-btn:not(:disabled):active {
  transform: scale(0.97);
}

.result-summary {
  display: flex;
  gap: 1.5rem;
  font-size: 0.95rem;
  padding-top: 0.8rem;
  margin-top: 0.9rem;
  border-top: 1px solid var(--border);
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.history-list li {
  font-size: 0.82rem;
  color: var(--muted);
  border-bottom: 1px dashed var(--border);
  padding-bottom: 0.35rem;
}

.history-list .history-time {
  color: var(--text);
  opacity: 0.6;
  margin-right: 0.4rem;
}

.site-footer {
  text-align: center;
  padding: 1.5rem 1rem 2.5rem;
  color: var(--muted);
  font-size: 0.78rem;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.6rem;
  position: relative;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}

.overlay[hidden] {
  display: none;
}

@media (min-width: 600px) {
  .overlay { align-items: center; }
}

.settings-panel {
  background: var(--card-bg);
  color: var(--text);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 14px 14px 0 0;
  padding: 1.25rem 1.25rem 2rem;
}

@media (min-width: 600px) {
  .settings-panel { border-radius: 14px; }
}

.settings-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.settings-panel-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.settings-section {
  margin-bottom: 1rem;
}

.settings-section h3 {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0 0 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  font-size: 0.92rem;
}

.row[hidden] {
  display: none;
}

.row select, .row input[type="number"] {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 0.35rem 0.5rem;
}

.accordion-toggle {
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0.9rem 0 0.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.chevron {
  display: inline-block;
  transition: transform 0.15s ease;
  font-size: 0.7rem;
}

.accordion-toggle.open .chevron {
  transform: rotate(90deg);
}

.advanced-section {
  background: var(--bg);
  border-radius: 10px;
  padding: 0.9rem;
}

.accordion-toggle.nested {
  border-top: none;
  padding: 0.5rem 0 0.4rem;
  margin-top: 0.3rem;
}

.outcome-section {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem;
  margin-top: 0.2rem;
}

.weight-die-block {
  margin-bottom: 0.9rem;
}

.weight-die-block h4 {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
}

.weight-preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.6rem;
}

.weight-preset-row button {
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  font-size: 0.76rem;
  cursor: pointer;
}

.weight-preset-row button:hover {
  border-color: var(--accent);
}

.weight-chart-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  padding: 0.5rem;
  margin-bottom: 0.35rem;
}

.weight-chart {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 100px;
  min-width: 100%;
  width: fit-content;
  touch-action: none;
}

.weight-bar {
  background: var(--accent);
  border-radius: 2px 2px 0 0;
  opacity: 0.6;
  min-height: 2px;
  cursor: pointer;
}

.weight-bar:hover {
  opacity: 0.85;
}

.weight-bar.zero {
  background: var(--border);
  opacity: 1;
}

.weight-chart-hint {
  font-size: 0.74rem;
  color: var(--muted);
  margin: 0 0 0.7rem;
}

.weight-range-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.35rem;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

.weight-range-row input[type="number"] {
  width: 52px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.35rem;
  background: var(--card-bg);
  color: var(--text);
}

.weight-range-row input[type="range"] {
  width: 90px;
}

.weight-range-row button {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 0.3rem 0.6rem;
  cursor: pointer;
}

.force-die-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  margin-bottom: 0.4rem;
}

.force-die-row input {
  width: 60px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.4rem;
  background: var(--card-bg);
  color: var(--text);
}

.cheat-btn-row {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.cheat-btn-row button {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  border-radius: 8px;
  padding: 0.5rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.cheat-btn-row button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.field-inline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  margin-bottom: 0.5rem;
}

.field-inline input[type="number"] {
  width: 80px;
}

.field-inline input[type="range"] {
  flex: 1;
}

.muted-note {
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0.3rem 0 0.6rem;
}
