*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: #0a0a0a;
  color: #e0e0e0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  touch-action: none;
}

#grid {
  display: block;
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
}

/* ── Stats bar (top-left) ── */
#stats {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: rgba(18, 18, 18, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.3px;
  opacity: 0.8;
  z-index: 10;
  pointer-events: none;
}

.stats-sep { opacity: 0.4; }

/* ── Floating action bar ── */
#floating-bar {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(18, 18, 18, 0.9);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 22px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5), 0 2px 8px rgba(0, 0, 0, 0.3);
  z-index: 30;
}

.fab-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.08);
  color: #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.15s, color 0.15s;
  outline: none;
}

.fab-btn:focus-visible { box-shadow: 0 0 0 2px #48c76e; }
.fab-btn:active { transform: scale(0.85); }

.fab-btn.primary {
  width: 48px;
  height: 48px;
  background: #2d7d46;
  color: #fff;
  border-radius: 16px;
}

.fab-btn.primary:active { background: #236b38; }
.fab-btn.primary.active { background: #c0392b; }
.fab-btn.primary.active:active { background: #a33025; }

/* ── Scrim overlay ── */
#scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  z-index: 19;
  transition: opacity 0.3s;
}

#scrim.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ── Bottom sheet ── */
#bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 85vh;
  background: rgba(28, 28, 30, 0.97);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.4);
  z-index: 20;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  overflow: hidden;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

#bottom-sheet.half-open {
  transform: translateY(0);
}

#bottom-sheet.full-open {
  transform: translateY(0);
}

#bottom-sheet.dragging {
  transition: none;
}

/* ── Sheet handle ── */
#sheet-handle {
  display: flex;
  justify-content: center;
  padding: 12px 0 8px;
  cursor: grab;
  touch-action: none;
}

.handle-bar {
  width: 36px;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
}

/* ── Sheet content ── */
#sheet-content {
  position: relative;
  overflow: hidden;
}

#sheet-main, #sheet-patterns {
  padding: 0 20px;
}

#sheet-main {
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.3s;
}

#sheet-patterns {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.3s;
  opacity: 0;
  padding-top: 4px;
}

#sheet-content.patterns-view #sheet-main {
  transform: translateX(-30%);
  opacity: 0;
  pointer-events: none;
}

#sheet-content.patterns-view #sheet-patterns {
  transform: translateX(0);
  opacity: 1;
}

/* ── Sheet sections ── */
.sheet-section {
  padding: 12px 0;
}

.sheet-section + .sheet-section {
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

/* ── Speed row ── */
.speed-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.speed-label {
  font-size: 13px;
  opacity: 0.5;
  flex-shrink: 0;
  min-width: 40px;
}

.speed-row input[type="range"] {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 2px;
  outline: none;
}

.speed-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #2d7d46;
  border: none;
  cursor: pointer;
}

.speed-row input[type="range"]::-moz-range-thumb {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #2d7d46;
  border: none;
  cursor: pointer;
}

#speed-value {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  opacity: 0.6;
  min-width: 20px;
  text-align: right;
}

/* ── Action button grid ── */
.action-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.action-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 8px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 14px;
  color: #ccc;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.15s;
  outline: none;
  min-height: 64px;
}

.action-btn:active {
  transform: scale(0.93);
  background: rgba(255, 255, 255, 0.12);
}

.action-btn:focus-visible { box-shadow: 0 0 0 2px #48c76e; }

.action-btn.small {
  min-height: auto;
  padding: 8px;
  flex-direction: row;
  gap: 4px;
}

/* ── Settings section ── */
.settings-section {
  padding-top: 16px;
}

#bottom-sheet.half-open .settings-section {
  display: none;
}

.settings-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.35;
  margin-bottom: 12px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
}

.setting-name {
  font-size: 15px;
  color: #ddd;
}

/* ── Segmented control ── */
.segmented-control {
  display: flex;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  padding: 2px;
  gap: 2px;
}

.seg-btn {
  padding: 8px 20px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: rgba(255, 255, 255, 0.5);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.seg-btn.active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

/* ── Toggle switch ── */
.toggle-switch {
  width: 50px;
  height: 28px;
  border-radius: 14px;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  cursor: pointer;
  position: relative;
  transition: background 0.25s;
  padding: 0;
}

.toggle-switch.on { background: #2d7d46; }

.toggle-knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
}

.toggle-switch.on .toggle-knob { transform: translateX(22px); }

/* ── Patterns sub-view ── */
.patterns-back {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: #48c76e;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  padding: 8px 0 16px;
}

.pattern-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.pattern-item {
  border: none;
  background: rgba(255, 255, 255, 0.04);
  color: #ddd;
  padding: 14px 16px;
  border-radius: 12px;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s;
  font-family: inherit;
}

.pattern-item:active { background: rgba(255, 255, 255, 0.12); }
.pattern-item:focus-visible { box-shadow: 0 0 0 2px #48c76e; outline: none; }

.pattern-rotate {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 0 8px;
}

#rotate-label {
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  opacity: 0.6;
}

/* ── Toast notification ── */
#toast {
  position: fixed;
  top: max(48px, calc(env(safe-area-inset-top) + 40px));
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  padding: 8px 18px;
  background: rgba(18, 18, 18, 0.9);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  font-size: 13px;
  color: #e0e0e0;
  opacity: 0;
  pointer-events: none;
  z-index: 40;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
}

#toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Gesture hints overlay ── */
#gesture-hints {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 1;
  transition: opacity 0.3s;
}

#gesture-hints.hidden {
  opacity: 0;
  pointer-events: none;
}

.hint-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
  align-items: center;
  padding: 32px;
  max-width: 300px;
}

.hint-row {
  display: flex;
  align-items: center;
  gap: 16px;
  color: #e0e0e0;
  font-size: 15px;
  line-height: 1.4;
}

.hint-icon {
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.7;
}

.btn-primary-lg {
  margin-top: 8px;
  padding: 12px 36px;
  font-size: 16px;
  font-family: inherit;
  border: none;
  border-radius: 14px;
  background: #2d7d46;
  color: #fff;
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.btn-primary-lg:active { transform: scale(0.93); }

/* ── Light mode ── */
@media (prefers-color-scheme: light) {
  html, body { background: #f0f0f0; }
  #stats {
    background: rgba(245, 245, 245, 0.8);
    border-color: rgba(0, 0, 0, 0.06);
    color: #333;
  }
  #floating-bar {
    background: rgba(245, 245, 245, 0.92);
    border-color: rgba(0, 0, 0, 0.08);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
  .fab-btn { background: rgba(0, 0, 0, 0.06); color: #555; }
  #scrim { background: rgba(0, 0, 0, 0.2); }
  #bottom-sheet {
    background: rgba(245, 245, 245, 0.97);
    box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.12);
  }
  .handle-bar { background: rgba(0, 0, 0, 0.15); }
  .action-btn {
    background: rgba(0, 0, 0, 0.04);
    border-color: rgba(0, 0, 0, 0.06);
    color: #555;
  }
  .action-btn:active { background: rgba(0, 0, 0, 0.08); }
  .setting-name { color: #333; }
  .segmented-control { background: rgba(0, 0, 0, 0.06); }
  .seg-btn { color: rgba(0, 0, 0, 0.4); }
  .seg-btn.active { background: rgba(0, 0, 0, 0.08); color: #000; }
  .toggle-switch { background: rgba(0, 0, 0, 0.12); }
  .pattern-item { background: rgba(0, 0, 0, 0.03); color: #333; }
  .pattern-item:active { background: rgba(0, 0, 0, 0.08); }
  .sheet-section + .sheet-section { border-color: rgba(0, 0, 0, 0.06); }
  #toast {
    background: rgba(245, 245, 245, 0.92);
    border-color: rgba(0, 0, 0, 0.08);
    color: #333;
  }
  #gesture-hints { background: rgba(255, 255, 255, 0.8); }
  .hint-row { color: #333; }
}
