:root {
  --bg-0: #f6f0e7;
  --bg-1: #e8d7c0;
  --ink: #1f2a37;
  --text-primary: #E8F4FD;
  --panel: #fffaf2;
  --accent: #de6e4b;
  --accent-soft: #f3c9b7;
  --ring-a: #2f4858;
  --ring-b: #f26430;
  --paper-colour: #ffffff;
  --panel-w: 330px;
  --tab-w: 26px;
  --history-controls-size: 42px;
  /* iOS safe area insets - populated by env() and used throughout */
  --safe-area-top: env(safe-area-inset-top);
  --safe-area-bottom: env(safe-area-inset-bottom);
  --safe-area-left: env(safe-area-inset-left);
  --safe-area-right: env(safe-area-inset-right);
}

html {
  width: 100%;
  height: 100%;
  min-height: 100vh;
  min-height: var(--actual-vh, 100vh);
  min-height: max(100vh, var(--actual-vh, 100vh));
  background: #1A2F42;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  min-height: var(--actual-vh, 100vh);
  min-height: max(100vh, var(--actual-vh, 100vh));
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 10%, #fff7ed 0 8%, transparent 9%),
    radial-gradient(circle at 92% 90%, #f5dec9 0 10%, transparent 11%),
    linear-gradient(155deg, var(--bg-0), var(--bg-1));
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

@media (hover: none) and (pointer: coarse) {
  html,
  body,
  body * {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }
}

.layout {
  position: fixed;
  inset: 0;
  display: block;
  width: 100vw;
  height: 100vh;
  height: var(--actual-vh, 100vh);
  min-height: 100vh;
  min-height: var(--actual-vh, 100vh);
  min-height: max(100vh, var(--actual-vh, 100vh));
  margin: 0;
  padding: 0;
}

.panel {
  position: fixed;
  left: 0;
  top: 0;
  width: var(--panel-w);
  height: 100vh;
  height: var(--actual-vh, 100vh);
  min-height: 100vh;
  min-height: var(--actual-vh, 100vh);
  min-height: max(100vh, var(--actual-vh, 100vh));
  overflow: visible;
  z-index: 20;
  transition: transform 0.26s ease;
}

.layout.panel-hidden .panel {
  transform: translateX(-100%);
}

.panel-inner {
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: rgba(26, 47, 66, 0.95);
  backdrop-filter: blur(5px);
  border-right: 2px solid rgba(255, 255, 255, 0.18);
  /* Safe area padding: ensures content clears notch/home bar on iPhone */
  padding: 20px;
  padding-top: max(20px, calc(var(--safe-area-top, 0px) + 10px));
  padding-bottom: max(20px, calc(var(--safe-area-bottom, 0px) + 10px));
  display: flex;
  flex-direction: column;
  gap: 4px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.panel-inner::-webkit-scrollbar {
  display: none;
}

.panel-tab {
  position: absolute;
  top: 50%;
  right: calc(-1 * var(--tab-w));
  transform: translateY(-50%);
  width: var(--tab-w);
  height: 88px;
  background: rgba(26, 47, 66, 0.95);
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-left: none;
  border-radius: 0 10px 10px 0;
  cursor: pointer;
  padding: 0;
  box-shadow: 3px 0 8px rgba(0, 0, 0, 0.35);
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 0;
  color: transparent;
}

.panel-tab::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  border-top: 2px solid rgba(232, 244, 253, 0.68);
  border-right: 2px solid rgba(232, 244, 253, 0.68);
  transform: rotate(45deg);
  margin: auto;
  transition: transform 0.26s ease;
}

.layout:not(.panel-hidden) .panel-tab::after {
  transform: rotate(225deg);
}

.panel-tab:hover {
  background: rgba(26, 47, 66, 0.95);
  border-color: rgba(255, 255, 255, 0.18);
}

.panel-logo {
  width: 100%;
  height: auto;
  margin-bottom: 0;
  -webkit-user-drag: none;
  pointer-events: none;
}

.subtitle {
  margin-top: -2px;
  margin-bottom: 1rem;
  font-size: 0.84rem;
  color: #ffffff;
  text-align: center;
}

label {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 0.8rem;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #ffffff;
}

input,
select,
button {
  font: inherit;
}

select {
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: #3A4F6A;
  color: #E8F4FD;
  padding: 10px 12px;
  appearance: none;
}

.radio-group {
  border: 1px solid #ffffff;
  border-radius: 8px;
  padding: 12px;
  padding-top: 8px;
  margin: 0 0 0.8rem 0;
}

.radio-group.is-disabled {
  opacity: 0.58;
}

.radio-group.is-disabled .radio-options label {
  cursor: not-allowed;
}

.radio-group legend {
  display: block;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #ffffff;
  margin: -8px 0 0.5rem -12px;
  padding: 0 8px;
  width: auto;
}

.radio-options {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.radio-options label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0;
  padding-left: 16px;
  font-size: 0.75rem;
  text-transform: none;
  letter-spacing: normal;
  cursor: pointer;
}

.spectra-option-text {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

.spectra-swatch {
  display: inline-block;
  width: 30px;
  height: 9px;
  border-radius: 999px;
  background: linear-gradient(90deg, #ff001e 0%, #ff6a00 24%, #ffd500 44%, #00c853 66%, #006dff 100%);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.38);
  transform: translateY(2px);
}

input[type="radio"] {
  cursor: pointer;
  accent-color: #4A90E2;
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

input[type="color"] {
  width: 100%;
  height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  padding: 2px;
  cursor: pointer;
  background: none;
}

.colour-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 0.8rem;
}

.colour-row.is-single {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.colour-row label {
  margin-bottom: 0;
}

input[type="range"] {
  accent-color: #4A90E2;
}

.actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 0.5rem;
  align-items: stretch;
}

.share-link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  letter-spacing: 0.04em;
  font-size: 10px;
  font-weight: 700;
}

.share-link-btn svg {
  flex: 0 0 auto;
}

button {
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 4px;
  background: #2A3F5A;
  color: #E8F4FD;
  font-size: 12px;
  line-height: 1.2;
  min-height: 40px;
  padding: 8px;
  text-align: center;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  box-sizing: border-box;
  transition: background 0.2s ease;
}

button.is-off {
  background: #556270;
  color: rgba(232, 244, 253, 0.72);
  border-color: rgba(255, 255, 255, 0.12);
}

@media (hover: hover) {
  button:hover {
    background: #354d6b;
  }

  button.is-off:hover {
    background: #5f6b78;
  }
}

.gear-note {
  margin: 0.2rem 0 0.6rem;
  font-size: 0.78rem;
  color: rgba(232, 244, 253, 0.68);
}

.stage-wrap {
  position: fixed;
  inset: 0;
  width: 100vw;
  min-height: 0;
  height: 100vh;
  height: var(--actual-vh, 100vh);
  min-height: 100vh;
  min-height: var(--actual-vh, 100vh);
  min-height: max(100vh, var(--actual-vh, 100vh));
  border-radius: 0;
  background: var(--paper-colour, #ffffff);
  border: 0;
  box-shadow: none;
  overflow: hidden;
}

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

.rack-rotate-control {
  position: absolute;
  right: max(14px, calc(var(--safe-area-right, 0px) + 8px));
  bottom: max(14px, calc(var(--safe-area-bottom, 0px) + 8px));
  width: min(280px, calc(100vw - 24px));
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  background: rgba(26, 47, 66, 0.88);
  color: #e8f4fd;
  backdrop-filter: blur(3px);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.25);
  display: none;
  z-index: 15;
}

.rack-rotate-control.is-visible {
  display: block;
}

.canvas-context-menu {
  position: absolute;
  min-width: 156px;
  padding: 6px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  background: rgba(26, 47, 66, 0.95);
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.35);
  z-index: 25;
  display: none;
}

.canvas-context-menu.is-open {
  display: block;
}

.canvas-context-menu button {
  width: 100%;
  min-height: 34px;
  border-radius: 6px;
  font-size: 11px;
  letter-spacing: 0.03em;
}

.history-controls {
  position: absolute;
  right: max(14px, calc(var(--safe-area-right, 0px) + 8px));
  bottom: max(14px, calc(var(--safe-area-bottom, 0px) + 8px));
  z-index: 18;
  display: inline-flex;
  gap: 8px;
}

.history-btn {
  width: var(--history-controls-size);
  height: var(--history-controls-size);
  border: 1px solid #555;
  border-radius: 8px;
  background: rgba(42, 63, 90, 0.95);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: filter 0.2s ease, opacity 0.2s ease, transform 0.15s ease;
  backdrop-filter: blur(6px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  min-height: 0;
  padding: 0;
  text-transform: none;
}

@media (hover: hover) {
  .history-btn:hover:not(:disabled) {
    filter: brightness(1.15);
  }
}

.history-btn:active:not(:disabled) {
  transform: translateY(1px);
}

.history-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.rack-rotate-control.is-visible ~ .history-controls {
  bottom: max(104px, calc(var(--safe-area-bottom, 0px) + 96px));
}

.rack-rotate-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 0 8px;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #e8f4fd;
}

.rack-rotate-value {
  font-size: 0.86rem;
  font-weight: 700;
  letter-spacing: normal;
  text-transform: none;
}

#rackRotateSlider {
  width: 100%;
  accent-color: #ffd700;
}

@media (max-width: 980px) {
  .rack-rotate-control {
    width: min(300px, calc(100vw - 20px));
    right: max(10px, calc(var(--safe-area-right, 0px) + 6px));
    bottom: max(10px, calc(var(--safe-area-bottom, 0px) + 6px));
  }

  .history-controls {
    right: max(10px, calc(var(--safe-area-right, 0px) + 6px));
    bottom: max(10px, calc(var(--safe-area-bottom, 0px) + 6px));
  }

  .rack-rotate-control.is-visible ~ .history-controls {
    bottom: max(112px, calc(var(--safe-area-bottom, 0px) + 102px));
  }
}

.perf-hud {
  position: fixed;
  right: 10px;
  top: 10px;
  z-index: 100;
  pointer-events: none;
  white-space: pre;
  font-family: Consolas, "Courier New", monospace;
  font-size: 11px;
  line-height: 1.35;
  color: #e8f4fd;
  background: rgba(10, 22, 34, 0.8);
  border: 1px solid rgba(232, 244, 253, 0.28);
  border-radius: 8px;
  padding: 8px 10px;
  backdrop-filter: blur(2px);
}

.help-close-btn {
  width: 20px;
  height: 20px;
  min-height: 20px;
  padding: 0;
  border: 1px solid #555;
  border-radius: 5px;
  background: #2A3F5A;
  color: #E8F4FD;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  text-transform: none;
}

@media (hover: hover) {
  .help-close-btn:hover {
    filter: brightness(1.15);
    box-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);
  }
}

.help-close-btn:active {
  background: #E74C3C;
}

.help-overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(5, 12, 18, 0.7);
  z-index: 2500;
  padding: 20px;
}

.help-overlay.show {
  display: flex;
}

.help-content {
  width: min(520px, 100%);
  max-height: min(88vh, calc(var(--actual-vh, 100vh) - 40px));
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 18px;
  background: rgba(26, 47, 66, 0.96);
  padding: 22px;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.help-header h2 {
  margin: 0;
  font-size: 1.02rem;
  color: #E8F4FD;
  letter-spacing: 0.02em;
}

.help-body {
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
}

.help-body::-webkit-scrollbar {
  width: 4px;
}

.help-body::-webkit-scrollbar-track {
  background: transparent;
}

.help-body::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 2px;
}

@media (max-width: 768px) {
  .help-body::-webkit-scrollbar {
    display: none;
  }

  .help-body {
    scrollbar-width: none;
  }
}

.help-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
  color: #E8F4FD;
  line-height: 1.5;
}

.help-section p {
  margin: 0;
}

.help-section h3 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  color: #E8F4FD;
}

.help-section + .help-section {
  border-top: 1px solid rgba(255, 255, 255, 0.18);
  margin-top: 14px;
  padding-top: 14px;
}

.about-startup-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  font-size: 1rem;
  line-height: 1.5;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  color: #E8F4FD;
  cursor: pointer;
}

.about-startup-checkbox {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  margin: 0;
  border: 2px solid #E8F4FD;
  border-radius: 4px;
  background: transparent;
  display: inline-grid;
  place-content: center;
  cursor: pointer;
}

.about-startup-checkbox::after {
  content: "";
  width: 5px;
  height: 9px;
  border-right: 2px solid #000000;
  border-bottom: 2px solid #000000;
  transform: rotate(45deg) scale(0);
  transform-origin: center;
  transition: transform 120ms ease-out;
}

.about-startup-checkbox:checked {
  background: #FFD700;
  border-color: #FFD700;
}

.about-startup-checkbox:checked::after {
  transform: rotate(45deg) scale(1);
}

.about-startup-checkbox:focus-visible {
  outline: 2px solid rgba(255, 215, 0, 0.45);
  outline-offset: 2px;
}

.help-section a {
  color: #FFD700;
  text-decoration: underline;
  cursor: pointer;
  font-weight: 600;
}

.help-section a:hover {
  color: #FFED4E;
}

@media (max-width: 980px) {
  .panel {
    width: min(82vw, var(--panel-w));
  }

  .stage-wrap {
    min-height: 0;
    height: 100vh;
    height: var(--actual-vh, 100vh);
    min-height: 100vh;
    min-height: var(--actual-vh, 100vh);
    min-height: max(100vh, var(--actual-vh, 100vh));
    border-radius: 0;
  }
}
