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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --border: rgba(255, 255, 255, 0.08);
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.45);
  --accent: #ff6b35;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  overflow: hidden;
}

/* Ruler runs down the left edge, top to bottom */
#ruler {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  color: var(--accent);
  display: block;
  pointer-events: none;
  z-index: 1;
}

/* Top bar sits at top-right, clear of the ruler's left edge */
.top-bar {
  position: fixed;
  top: env(safe-area-inset-top, 0);
  right: env(safe-area-inset-right, 0);
  padding: 0.85rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  z-index: 20;
}

.back-link {
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  text-decoration: none;
  padding: 0.4rem 0.5rem;
  transition: color 0.15s ease;
}
.back-link:hover { color: var(--text); }

.unit-toggle {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
}
.unit-toggle button {
  background: transparent;
  border: 0;
  color: var(--muted);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.32rem 0.85rem;
  border-radius: 999px;
  cursor: pointer;
  letter-spacing: 0.05em;
  transition: color 0.15s, background 0.15s;
}
.unit-toggle button.active {
  background: var(--accent);
  color: #0f0f0f;
}

.cal-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font: inherit;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  padding: 0.42rem 0.7rem;
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.cal-btn:hover { color: var(--text); border-color: var(--muted); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  overflow-y: auto;
}
.modal[hidden] { display: none; }

.modal-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.5rem;
  max-width: 400px;
  width: 100%;
}

.modal-panel h2 {
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  margin-bottom: 0.5rem;
}

.modal-hint {
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.55;
  margin-bottom: 1.25rem;
}
.modal-hint.small {
  font-size: 0.72rem;
  margin: 0.5rem 0 1rem;
}

.field-label {
  display: block;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.4rem;
}

.modal-panel select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  padding: 0.6rem 0.75rem;
  border-radius: 6px;
  margin-bottom: 1rem;
  appearance: none;
  -webkit-appearance: none;
}

.manual-cal {
  margin-top: 0.25rem;
  border-top: 1px solid var(--border);
  padding-top: 0.4rem;
}
.manual-cal > summary {
  list-style: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 0.5rem 0;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  transition: color 0.15s;
}
.manual-cal > summary:hover { color: var(--text); }
.manual-cal > summary::-webkit-details-marker { display: none; }
.manual-cal > summary::before {
  content: '›';
  display: inline-block;
  font-size: 1.15rem;
  line-height: 0.9;
  transition: transform 0.15s;
}
.manual-cal[open] > summary::before {
  transform: rotate(90deg);
}
.manual-cal[open] {
  padding-bottom: 0.4rem;
}
.manual-cal > *:not(summary) {
  margin-top: 0.5rem;
}

.ref-stage {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 110px;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  margin: 0.5rem 0 1rem;
  overflow: hidden;
}

.ref-shape {
  background: var(--accent);
  opacity: 0.9;
  flex-shrink: 0;
}

.modal-panel input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.2rem;
}

.btn-primary,
.btn-secondary {
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 0.6rem 1.15rem;
  border-radius: 6px;
  cursor: pointer;
  border: 1px solid var(--border);
  letter-spacing: 0.02em;
}
.btn-primary {
  background: var(--accent);
  color: #0f0f0f;
  border-color: var(--accent);
}
.btn-secondary {
  background: transparent;
  color: var(--muted);
}
.btn-secondary:hover { color: var(--text); }
