:root {
  color-scheme: light;
  --ink: #1f2430;
  --muted: #626b7a;
  --panel: rgba(255, 255, 255, 0.88);
  --panel-strong: rgba(255, 255, 255, 0.96);
  --line: rgba(31, 36, 48, 0.14);
  --accent: #0f8f83;
  --accent-strong: #086b63;
  --danger: #c43c35;
  --shadow: 0 18px 55px rgba(23, 30, 45, 0.18);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #dfe6e9;
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.app-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  min-height: 560px;
}

#scene {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.startup-error {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 20;
  width: min(440px, calc(100vw - 32px));
  padding: 18px;
  border: 1px solid rgba(196, 60, 53, 0.28);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
  color: var(--danger);
  line-height: 1.5;
  transform: translate(-50%, -50%);
}

.startup-error strong,
.startup-error span {
  display: block;
}

.startup-error span {
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.9rem;
}

.control-panel {
  position: absolute;
  top: 18px;
  left: 18px;
  width: min(380px, calc(100vw - 36px));
  display: grid;
  gap: 14px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.brand-row {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 14px;
}

.brand-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.mobile-panel-toggle {
  display: none;
}

.panel-body {
  display: contents;
}

.kicker,
.brand-row h1,
.status-lines p {
  margin: 0;
}

.kicker {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0;
}

.brand-row h1 {
  margin-top: 2px;
  font-size: clamp(1.45rem, 4vw, 2rem);
  line-height: 1;
}

.count-pill {
  min-width: 58px;
  padding: 7px 10px;
  border-radius: 999px;
  background: #18202b;
  color: #fff;
  font-size: 0.86rem;
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
}

.toolbar,
.view-grid,
.rotate-grid {
  display: grid;
  gap: 8px;
}

.toolbar {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.segmented {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(24, 32, 43, 0.06);
}

.tool-button,
.icon-button,
.view-grid button,
.rotate-grid button {
  min-height: 42px;
  border-radius: 7px;
  color: var(--ink);
  background: var(--panel-strong);
  transition:
    background 140ms ease,
    color 140ms ease,
    transform 140ms ease,
    box-shadow 140ms ease;
}

.tool-button,
.view-grid button,
.rotate-grid button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 7px;
  font-size: 0.9rem;
  font-weight: 700;
  min-width: 0;
  white-space: nowrap;
}

.tool-button svg,
.icon-button svg,
.view-grid button svg,
.rotate-grid button svg {
  width: 18px;
  height: 18px;
  stroke-width: 2.2;
  flex: 0 0 auto;
}

.tool-button:hover,
.icon-button:hover,
.view-grid button:hover,
.rotate-grid button:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 18px rgba(20, 29, 40, 0.12);
}

.tool-button.is-active {
  background: var(--accent);
  color: #fff;
}

.field,
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 42px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-strong);
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.field input {
  width: 84px;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: #fff;
  text-align: right;
}

.toggle-row {
  justify-content: start;
}

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

.icon-button {
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
}

.icon-button.danger {
  color: var(--danger);
}

.view-grid,
.rotate-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.view-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.view-grid button,
.rotate-grid button {
  border: 1px solid var(--line);
}

.rotate-panel {
  display: grid;
  gap: 8px;
}

.panel-label {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.status-lines {
  display: grid;
  gap: 5px;
  padding: 10px 12px;
  border-radius: 7px;
  background: rgba(24, 32, 43, 0.07);
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.35;
}

@media (max-width: 720px), (max-width: 950px) and (max-height: 500px) {
  .app-shell {
    height: 100vh;
    height: 100dvh;
    min-height: 0;
  }

  .control-panel {
    top: auto;
    right: auto;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 50%;
    width: min(
      680px,
      calc(
        100vw - 16px - env(safe-area-inset-left) -
          env(safe-area-inset-right)
      )
    );
    max-height: calc(100dvh - 16px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 10px;
    gap: 8px;
    background: rgba(255, 255, 255, 0.94);
    transform: translateX(-50%);
  }

  .brand-row {
    align-items: center;
    min-height: 38px;
  }

  .kicker {
    display: none;
  }

  .brand-row h1 {
    margin: 0;
    font-size: 1.18rem;
  }

  .count-pill {
    min-width: 52px;
    padding: 6px 9px;
    font-size: 0.8rem;
  }

  .mobile-panel-toggle {
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 0 8px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--panel-strong);
    color: var(--ink);
    font-size: 0.76rem;
    font-weight: 800;
  }

  .mobile-panel-toggle svg {
    width: 17px;
    height: 17px;
    transition: transform 180ms ease;
  }

  .control-panel.is-expanded .mobile-panel-toggle svg {
    transform: rotate(180deg);
  }

  .segmented {
    min-height: 48px;
    gap: 4px;
    padding: 3px;
  }

  .tool-button {
    min-height: 42px;
    padding: 0 4px;
  }

  .panel-body {
    min-height: 0;
    max-height: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    overflow: hidden;
    opacity: 0;
    transition:
      max-height 180ms ease,
      opacity 140ms ease;
  }

  .control-panel.is-expanded .panel-body {
    max-height: min(64dvh, 430px);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    opacity: 1;
    padding-top: 2px;
    padding-right: 2px;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }

  .field,
  .toggle-row {
    min-width: 0;
    min-height: 44px;
    padding: 7px 9px;
    font-size: 0.78rem;
  }

  .field input {
    width: 64px;
    height: 32px;
  }

  .toggle-row {
    gap: 7px;
  }

  .toggle-row input {
    width: 20px;
    height: 20px;
    flex: 0 0 auto;
  }

  .edit-toolbar,
  .rotate-panel,
  .view-grid,
  .status-lines {
    grid-column: 1 / -1;
  }

  .edit-toolbar,
  .view-grid,
  .rotate-grid {
    gap: 6px;
  }

  .icon-button {
    min-height: 44px;
  }

  .tool-button,
  .view-grid button,
  .rotate-grid button {
    min-height: 44px;
    gap: 4px;
    padding: 0 5px;
    font-size: 0.78rem;
  }

  .panel-label {
    font-size: 0.76rem;
  }

  .status-lines {
    padding: 8px 10px;
    font-size: 0.76rem;
  }

}

@media (max-width: 380px) {
  .mobile-panel-toggle span {
    display: none;
  }

  .mobile-panel-toggle {
    width: 38px;
    padding: 0;
  }

  .tool-button {
    font-size: 0.72rem;
  }

  .view-grid button span {
    display: none;
  }
}
