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

:root,
[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f1ec;
  --fg: #1a1d22;
  --muted: #595f68;
  --dim: #9299a2;
  --border: rgba(26, 29, 34, 0.12);
  --border-soft: rgba(26, 29, 34, 0.06);
  --field-bg: rgba(26, 29, 34, 0.03);
  --field-border: rgba(26, 29, 34, 0.18);
  --btn-bg: rgba(26, 29, 34, 0.05);
  --btn-bg-hover: rgba(26, 29, 34, 0.09);
  --btn-border: rgba(26, 29, 34, 0.18);
  --header-border: rgba(26, 29, 34, 0.12);
  --link: #2c5a7a;
  --accent: #2c5a7a;
  --nav-bg: rgba(244, 241, 236, 0.6);
  --bg-panel: rgba(255, 253, 250, 0.72);
  --notice-fg: #2a6e2a;
  --notice-bg: rgba(42, 110, 42, 0.06);
  --notice-border: rgba(42, 110, 42, 0.25);
  --error-fg: #a03030;
  --error-bg: rgba(160, 48, 48, 0.06);
  --error-border: rgba(160, 48, 48, 0.25);
  --toggle-bg: linear-gradient(135deg, #07070d 0%, #64dcff 100%);
  --toggle-border: rgba(26, 29, 34, 0.2);
  --toggle-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

[data-theme="tokyo"] {
  color-scheme: dark;
  --bg: #07070d;
  --fg: #e8f4ff;
  --muted: #a3b3cc;
  --dim: #7886a0;
  --border: rgba(100, 220, 255, 0.12);
  --border-soft: rgba(100, 220, 255, 0.06);
  --field-bg: rgba(100, 220, 255, 0.04);
  --field-border: rgba(100, 220, 255, 0.15);
  --btn-bg: rgba(100, 220, 255, 0.06);
  --btn-bg-hover: rgba(100, 220, 255, 0.1);
  --btn-border: rgba(100, 220, 255, 0.18);
  --header-border: rgba(100, 220, 255, 0.12);
  --link: #64dcff;
  --accent: #64dcff;
  --nav-bg: rgba(7, 7, 13, 0.65);
  --bg-panel: rgba(15, 16, 28, 0.72);
  --notice-fg: #8fe08f;
  --notice-bg: rgba(143, 224, 143, 0.06);
  --notice-border: rgba(143, 224, 143, 0.25);
  --error-fg: #ff9b9b;
  --error-bg: rgba(255, 155, 155, 0.06);
  --error-border: rgba(255, 155, 155, 0.2);
  --toggle-bg: #f4f1ec;
  --toggle-border: rgba(100, 220, 255, 0.4);
  --toggle-shadow: 0 0 10px rgba(100, 220, 255, 0.5), 0 0 25px rgba(100, 220, 255, 0.3);
}

html, body { max-width: 100%; overflow-x: hidden; }
body {
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', monospace;
  font-size: 0.88rem;
  font-weight: 300;
  line-height: 1.55;
  color: var(--fg);
  background: var(--bg);
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background 0.3s ease, color 0.3s ease;
}

header {
  border-bottom: 1px solid var(--header-border);
  padding: 1rem 1.5rem;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  background: var(--nav-bg);
  backdrop-filter: blur(12px) saturate(120%);
  -webkit-backdrop-filter: blur(12px) saturate(120%);
  position: sticky;
  top: 0;
  z-index: 20;
}

main {
  overflow-wrap: break-word;
  padding: 16px;
  max-width: 1440px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(520px, 0.9fr) minmax(460px, 1.1fr);
  gap: 14px;
  align-items: start;
}

.interaction-pane {
  min-width: 0;
}

.back-link {
  font-weight: 400;
  font-size: 0.7rem;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--muted);
  text-decoration: none;
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 2px;
}
.back-link:hover { color: var(--fg); border-color: var(--muted); background: var(--bg-panel); }

.brand {
  justify-self: center;
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: -0.02em;
  color: var(--fg);
  text-decoration: none;
  display: inline-flex;
  align-items: baseline;
  gap: 0.35rem;
  white-space: nowrap;
}
.brand-sep { font-weight: 300; color: var(--dim); }
.brand-sub {
  font-weight: 400;
  font-size: 0.78rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}
[data-theme="tokyo"] .brand { color: #e8f4ff; text-shadow: 0 0 8px rgba(100, 220, 255, 0.3); }

footer {
  padding: 0.9rem 1.5rem;
  color: var(--muted);
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  border-top: 1px solid var(--border-soft);
  background: var(--nav-bg);
  display: flex;
  justify-content: center;
}
footer a, a { color: var(--link); }

h1, h2, h3 {
  font-family: inherit;
  font-weight: 700;
  letter-spacing: -0.02em;
}
h1 { margin: 0 0 0.9rem; font-size: clamp(1.35rem, 4vw, 2rem); }
h2 { margin: 0 0 0.9rem; font-size: 1.1rem; }
h3 { margin: 0 0 0.4rem; font-size: 0.9rem; }
p { margin: 0 0 0.8rem; }
code { font-family: inherit; font-size: 0.86em; }
.muted { color: var(--muted); font-size: 0.78rem; }

.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.inventory-shell,
.control-deck,
.cdc-pane {
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--field-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.inventory-shell {
  padding: 1rem;
}

.control-deck {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-top: 12px;
  padding: 12px;
}

.inventory-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.inventory-slot {
  font: inherit;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg-panel);
  color: var(--fg);
  display: flex;
  flex-direction: column;
  gap: 7px;
  min-height: 118px;
  padding: 0.75rem;
  text-align: left;
  min-width: 0;
  cursor: pointer;
}
.inventory-slot:hover { background: var(--btn-bg-hover); }
.inventory-slot.ok { border-color: var(--notice-border); }
.inventory-slot.low { border-color: rgba(185, 126, 0, 0.35); }
.inventory-slot.empty { border-color: var(--error-border); }
.inventory-slot.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.inventory-slot.changed {
  background: var(--notice-bg);
  border-color: var(--notice-border);
}
.inventory-slot strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.95rem;
}
.inventory-slot .quantity {
  display: inline-block;
  min-width: 1.8ch;
  font-size: 1.7rem;
  font-weight: 700;
}
.slot-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.create-slot {
  border-style: dashed;
  color: var(--muted);
  place-content: center;
  text-align: center;
}
.create-slot strong {
  color: var(--fg);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

fieldset {
  border: 1px solid var(--border);
  margin: 0;
  padding: 10px 12px;
  min-width: 0;
  border-radius: 3px;
  background: var(--field-bg);
}
legend {
  font-weight: 500;
  padding: 0 6px;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.selected-summary {
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg-panel);
  padding: 0.9rem 1rem;
  display: grid;
  grid-template-columns: 130px minmax(0, 1fr);
  align-items: center;
  gap: 16px;
}
.selected-summary.create-mode {
  border-style: dashed;
}
.selected-summary strong {
  display: block;
  font-size: 1.05rem;
  margin-bottom: 0.15rem;
}
.summary-kicker {
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.action-buttons {
  display: grid;
  gap: 7px;
}
.action-buttons.two-up {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.selected-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  align-items: start;
}

.selected-actions fieldset,
.create-actions fieldset {
  height: 100%;
}

.primary-action-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.primary-stock-action {
  min-height: 92px;
  display: grid;
  place-items: center;
  gap: 0.2rem;
  background: var(--bg-panel);
  border-color: var(--field-border);
}
.primary-stock-action span {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.primary-stock-action strong {
  font-size: 1.9rem;
  line-height: 1;
}

.more-actions {
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--field-bg);
  overflow: hidden;
}
.more-actions > summary {
  cursor: pointer;
  list-style: none;
  padding: 0.65rem 0.8rem;
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.more-actions > summary::-webkit-details-marker { display: none; }
.more-actions > summary::after {
  content: '+';
  float: right;
  color: var(--dim);
}
.more-actions[open] > summary {
  border-bottom: 1px solid var(--border);
  color: var(--fg);
}
.more-actions[open] > summary::after { content: '-'; }
.more-actions-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 170px;
  gap: 10px;
  padding: 10px;
}

.compact-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.compact-form-grid label:nth-child(3),
.compact-form-grid label:nth-child(4) {
  grid-column: span 1;
}

.create-actions {
  grid-column: 1 / -1;
}

.create-form-grid {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr) 100px;
  gap: 8px;
  align-items: end;
}

.create-form-grid label:nth-child(2) {
  grid-column: span 2;
}

.create-form-grid label:nth-child(4) {
  grid-column: span 2;
}

.create-form-grid label {
  margin-bottom: 0;
}

select, input[type=text], input[type=number], input:not([type]) {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--field-border);
  border-radius: 3px;
  background: var(--field-bg);
  color: var(--fg);
  outline: none;
  width: 100%;
}
[data-theme="tokyo"] select,
[data-theme="tokyo"] option { background: #0f1018; color: #e8f4ff; }
select:focus, input:focus { border-color: var(--accent); }
label { display: block; font-size: 0.78rem; color: var(--muted); margin-bottom: 0.75rem; }
label input, label select { margin-top: 0.25rem; }

.edit-actions button,
.danger-actions button {
  width: 100%;
}

button {
  font: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.3rem 0.7rem;
  border: 1px solid var(--btn-border);
  border-radius: 3px;
  background: var(--btn-bg);
  color: var(--fg);
  cursor: pointer;
  letter-spacing: 0.02em;
}
button:hover { background: var(--btn-bg-hover); }
button.danger { color: var(--error-fg); border-color: var(--error-border); background: var(--error-bg); }
.inline { display: inline-flex; gap: 6px; align-items: center; margin: 0; flex-wrap: wrap; }

.cdc-pane {
  position: sticky;
  top: 72px;
  max-height: calc(100vh - 96px);
  overflow: auto;
}

.cdc-heading {
  padding: 1rem 1rem 0;
}

.cdc-heading h2 {
  margin-bottom: 0;
}

.event-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  padding: 12px;
}
.detail-panel {
  grid-column: 1 / -1;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--field-bg);
  overflow: hidden;
}
.detail-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}
.detail-tab {
  border: 0;
  border-right: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  padding: 0.55rem 0.75rem;
}
.detail-tab.active {
  background: var(--btn-bg-hover);
  color: var(--fg);
}
.detail-view { display: none; padding: 0.75rem; }
.detail-view.active { display: block; }
pre {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-soft);
  border-radius: 3px;
  background: rgba(26, 29, 34, 0.04);
  margin: 0.8rem 0 0;
  padding: 0.85rem 0.95rem;
  font-size: 0.78rem;
  line-height: 1.45;
}
[data-theme="tokyo"] pre { background: rgba(100, 220, 255, 0.04); }
.json-output {
  max-height: 44vh;
  margin: 0;
}
.event-summary p { margin-bottom: 0.35rem; }

.audit-list {
  display: grid;
  gap: 8px;
}
.audit-row {
  display: grid;
  grid-template-columns: 90px 120px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  border: 1px solid var(--border-soft);
  border-radius: 3px;
  padding: 0.55rem 0.65rem;
  background: var(--bg-panel);
}
.audit-row span { color: var(--muted); }
.audit-row code {
  white-space: pre-wrap;
  color: var(--fg);
}

.theme-toggle {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--toggle-border);
  cursor: pointer;
  padding: 0;
  z-index: 30;
  transition: all 0.3s ease;
  box-shadow: var(--toggle-shadow);
  background: var(--toggle-bg);
}
.theme-toggle:hover { transform: scale(1.08); }

@media (max-width: 900px) {
  .workbench { grid-template-columns: 1fr; }
  .cdc-pane {
    position: static;
    max-height: none;
  }
  .event-grid { grid-template-columns: 1fr; }
  .selected-actions,
  .more-actions-panel,
  .create-form-grid,
  .compact-form-grid { grid-template-columns: 1fr; }
  .create-actions { grid-column: auto; }
  .primary-stock-action { min-height: 72px; }
}

@media (max-width: 760px) {
  header {
    grid-template-columns: 1fr;
    justify-items: start;
    padding: 0.7rem 0.9rem;
    gap: 0.5rem;
  }
  .brand { justify-self: start; font-size: 0.9rem; }
  main { padding: 12px; }
  footer { font-size: 0.62rem; padding: 0.7rem 0.9rem; }
  .theme-toggle { width: 20px; height: 20px; bottom: 1rem; right: 0.9rem; }
  .inventory-grid,
  .primary-action-row,
  .action-buttons.two-up { grid-template-columns: 1fr; }
  .inventory-slot { min-height: 98px; }
  .selected-summary { grid-template-columns: 1fr; gap: 4px; }
  .section-heading { align-items: flex-start; flex-direction: column; gap: 4px; }
  .audit-row { grid-template-columns: 1fr; gap: 2px; }
}
