:root {
  color-scheme: dark;
  --bg: #17191c;
  --surface: #202327;
  --surface-2: #282c31;
  --surface-3: #30353b;
  --line: #3d434b;
  --muted: #a2a9b1;
  --text: #f3f5f7;
  --accent: #20a84a;
  --accent-soft: rgba(32, 168, 74, 0.18);
  --warning: #c79d3b;
  --danger: #d66a5f;
  --zoom: 0.9;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Meiryo, system-ui, sans-serif;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 12px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}

button:hover {
  border-color: var(--accent);
}

input,
select {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 10px;
  background: var(--surface-2);
  color: var(--text);
  min-width: 0;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 18px 22px;
  border-bottom: 1px solid var(--line);
  background: #15171a;
}

.brand-block {
  min-width: 260px;
}

.eyebrow {
  margin: 0 0 4px;
  color: #8bdc9f;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.workspace-title {
  width: min(640px, 54vw);
  height: auto;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(28px, 4vw, 48px);
  font-weight: 700;
  line-height: 1.05;
}

.workspace-title:focus {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 4px;
}

.top-controls,
.button-row,
.toolbar-actions,
.loop-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.compact-field,
.text-field,
.range-field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.compact-field {
  min-width: 122px;
}

.tempo-field {
  width: 84px;
  min-width: 84px;
}

.app-shell {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  overflow: hidden;
}

.sidebar {
  min-height: 0;
  overflow: auto;
  padding: 16px;
  border-right: 1px solid var(--line);
  background: #1d2024;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--surface);
}

.panel + .panel {
  margin-top: 12px;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.panel-title h2 {
  margin: 0;
  font-size: 14px;
}

.panel-title span,
.panel-note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.drop-zone {
  display: grid;
  place-items: center;
  gap: 8px;
  min-height: 142px;
  padding: 18px;
  border: 1px dashed color-mix(in srgb, var(--muted) 60%, transparent);
  border-radius: 8px;
  text-align: center;
  cursor: pointer;
}

.drop-zone input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.drop-zone.is-over {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.drop-icon {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: #8bdc9f;
  font-size: 24px;
}

.drop-zone small {
  color: var(--muted);
  line-height: 1.5;
}

.button-row {
  margin-top: 10px;
}

.button-row button {
  flex: 1;
}

.page-rail,
.loop-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.page-chip,
.loop-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  background: transparent;
  text-align: left;
}

.page-chip small,
.loop-item small {
  color: var(--muted);
}

.page-chip.is-active,
.loop-item.is-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.range-field input {
  width: 100%;
  accent-color: var(--accent);
}

.toggle-row {
  display: grid;
  grid-template-columns: 18px 1fr;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.toggle-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.loop-panel {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.loop-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 64px;
  gap: 7px;
}

.loop-form input:first-child {
  grid-column: 1 / -1;
}

.loop-actions {
  margin-top: 12px;
}

.loop-actions button {
  flex: 1;
}

#loopPlayButton,
.toolbar-actions button:first-child {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 24%, var(--surface-2));
}

.bridge-panel pre {
  overflow: auto;
  margin: 10px 0;
  padding: 10px;
  border-radius: 7px;
  background: #121417;
  color: #d8dde3;
  font-size: 12px;
}

.workspace {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.viewer-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.viewer-toolbar span {
  margin-left: 10px;
  color: var(--muted);
  font-size: 13px;
}

.workspace-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
}

.score-viewport {
  min-height: 0;
  overflow: auto;
  padding: 18px;
  scroll-behavior: smooth;
  outline: none;
}

.score-viewport.is-looping {
  scroll-behavior: auto;
}

.score-stack {
  display: grid;
  justify-items: start;
  gap: 0;
  width: max-content;
  min-width: 100%;
  transform: scale(var(--zoom));
  transform-origin: top left;
}

.score-stack.show-gaps {
  gap: 16px;
}

.score-page {
  width: fit-content;
  background: #191b1f;
  outline: 1px solid transparent;
}

.score-page.is-active {
  outline-color: var(--accent);
}

.score-page.is-dragging {
  opacity: 0.5;
}

.page-label {
  display: none;
  align-items: center;
  gap: 10px;
  height: 30px;
  padding: 0 10px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
}

.score-stack.show-gaps .page-label,
.score-page.is-active .page-label {
  display: flex;
}

.drag-handle {
  color: #8bdc9f;
  cursor: grab;
}

.image-frame {
  overflow: hidden;
}

.image-frame img {
  display: block;
  width: var(--natural-width, 1800px);
  max-width: none;
  transform: translateY(calc(var(--crop-top, 0px) * -1));
  margin-bottom: calc(var(--crop-top, 0px) * -1 - var(--crop-bottom, 0px));
  user-select: none;
}

.empty-state {
  display: grid;
  place-items: center;
  align-content: center;
  min-height: 58vh;
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 32px;
  text-align: center;
  color: var(--muted);
}

.empty-state h2 {
  margin: 0 0 10px;
  color: var(--text);
  font-size: 22px;
}

.inspector {
  min-height: 0;
  overflow: auto;
  border-left: 1px solid var(--line);
  padding: 16px;
  background: #1d2024;
}

.inspector-body {
  display: grid;
  gap: 12px;
}

.measure-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.danger-button {
  border-color: color-mix(in srgb, var(--danger) 60%, var(--line));
  color: #ffd9d5;
}

@media (max-width: 1120px) {
  body {
    overflow: auto;
  }

  .topbar,
  .top-controls {
    align-items: stretch;
    flex-direction: column;
  }

  .workspace-title {
    width: 100%;
  }

  .top-controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .app-shell {
    grid-template-columns: 1fr;
    overflow: visible;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .workspace {
    min-height: 76vh;
  }
}

@media (max-width: 760px) {
  .top-controls,
  .workspace-grid {
    grid-template-columns: 1fr;
  }

  .inspector {
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .viewer-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .loop-form {
    grid-template-columns: 1fr 1fr;
  }
}

@media print {
  body {
    overflow: visible;
    background: #fff;
  }

  .topbar,
  .sidebar,
  .viewer-toolbar,
  .inspector,
  .empty-state {
    display: none !important;
  }

  .app-shell,
  .workspace,
  .workspace-grid {
    display: block;
  }

  .score-viewport {
    overflow: visible;
    padding: 0;
  }

  .score-stack {
    transform: none;
    width: 100%;
  }

  .image-frame img {
    width: 100%;
    break-inside: avoid;
  }
}
