:root {
  color-scheme: light;
  --card-bg: rgba(255, 252, 245, 0.91);
  --card-border: rgba(27, 48, 59, 0.14);
  --ink: #192d38;
  --ink-muted: #516875;
  --teal: #0d8599;
  --teal-soft: rgba(13, 133, 153, 0.13);
  --gold: #bd7615;
  --gold-soft: rgba(209, 140, 38, 0.18);
  --danger: #c63d2f;
  --shadow: 0 6px 20px rgba(24, 46, 58, 0.10);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  position: fixed;
  inset: 0;
  background: #87ceeb;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#world-gl,
#world-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#world-gl {
  touch-action: none;
  cursor: crosshair;
}

#world-overlay {
  pointer-events: none;
}

/* Top Sunlit HUD */
#hud-top {
  position: fixed;
  top: max(6px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  width: min(980px, calc(100vw - 12px));
  display: flex;
  flex-direction: column;
  gap: 5px;
  z-index: 10;
  pointer-events: none;
}

.top-bar {
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  background: var(--card-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 5px 8px;
  box-shadow: var(--shadow);
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.brand-mark {
  font-weight: 800;
  font-size: 13px;
  letter-spacing: -0.02em;
  color: var(--ink);
  padding: 0 4px;
  white-space: nowrap;
}

#island-picker {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  max-width: 100%;
  min-height: 32px;
  padding: 5px 12px;
  border: 1px solid #acd6cd;
  border-radius: 999px;
  background: #e7f5ef;
  color: #18594e;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

#island-current {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.picker-arrow {
  width: 6px;
  height: 6px;
  border: solid currentColor;
  border-width: 0 1.5px 1.5px 0;
  transform: translateY(-2px) rotate(45deg);
  flex-shrink: 0;
}

#island-drawer {
  position: fixed;
  inset: max(9px, env(safe-area-inset-top)) 0 auto;
  margin: 0 auto;
  width: min(742px, calc(100% - 20px));
  max-width: none;
  max-height: calc(100dvh - max(9px, env(safe-area-inset-top)) - max(9px, env(safe-area-inset-bottom)));
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y pinch-zoom;
  padding: 18px;
  color: var(--ink);
  background: rgba(255, 252, 241, .97);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid #cfdbcc;
  border-radius: 18px;
  box-shadow: 0 14px 38px rgba(67, 91, 84, .20);
}
.drawer-head, .weather-controls { display: flex; align-items: center; gap: 8px; }
.drawer-head { justify-content: space-between; }
.drawer-head h2 {
  margin: 0;
  font: 500 26px/1.2 Georgia, serif;
  color: #224f47;
}
#island-close {
  cursor: pointer;
  border-radius: 50%;
  border: 1px solid #ccd6c8;
  background: #fffcf4;
  color: #375f55;
  font-size: 18px;
  width: 36px;
  height: 36px;
}
.weather-controls {
  flex-wrap: wrap;
  padding: 10px 0 14px;
}
.weather-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 5px 10px;
  cursor: pointer;
  border: 1px solid #c8d8cd;
  border-radius: 99px;
  background: #fffcf4;
  color: #28594e;
  font-size: 13px;
  font-family: inherit;
  font-weight: 600;
}
.weather-toggle::after {
  content: 'Off';
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 99px;
  background: #f2eee3;
  color: #6c6758;
}
.weather-toggle[aria-pressed="true"] { border-color: #72ac9d; background: #e9f6ef; }
.weather-toggle[aria-pressed="true"]::after { content: 'On'; background: #d0eadd; color: #28594e; }
.weather-toggle:disabled { cursor: default; opacity: .7; }

#mode-switcher {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}
.island-option {
  position: relative;
  display: grid;
  align-content: center;
  gap: 3px;
  min-width: 0;
  min-height: 54px;
  padding: 8px 12px;
  text-align: left;
  font-family: inherit;
  cursor: pointer;
  color: #244b42;
  background: #fffdf7;
  border: 1px solid #e0ddcd;
  border-radius: 10px;
}
.island-option:hover { border-color: #9ebfaf; }
.island-option[aria-current="true"] {
  border-color: #72ac9d;
  background: #e9f6ef;
}
.island-option[aria-current="true"] strong::after {
  content: ' ✓';
  color: #286d5e;
}
.island-option strong { font-size: 14px; }
.island-goal { color: #526459; font-size: 12px; line-height: 1.3; }
button:focus-visible { outline: 3px solid #247a76; outline-offset: 1px; z-index: 1; }

.env-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.action-chip {
  appearance: none;
  border: 1px solid rgba(25, 45, 56, 0.14);
  background: #ffffff;
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
  padding: 5px 8px;
  border-radius: 9px;
  cursor: pointer;
  white-space: nowrap;
  line-height: 1.1;
}

.action-chip.wave-chip {
  background: #e6f6f9;
  border-color: rgba(13, 133, 153, 0.32);
  color: #096879;
}

.action-chip.is-active {
  background: #fff2d6;
  border-color: rgba(189, 118, 21, 0.45);
  color: #8f5607;
}

.objective-bar {
  pointer-events: auto;
  align-self: center;
  max-width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 252, 246, 0.88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink);
  box-shadow: 0 3px 10px rgba(24, 46, 58, 0.07);
}

#hud-top[data-status="won"] .objective-bar {
  background: rgba(234, 251, 243, 0.95);
  border-color: rgba(14, 159, 110, 0.42);
  color: #096949;
}

#hud-top[data-status="lost"] .objective-bar {
  background: rgba(255, 241, 238, 0.95);
  border-color: rgba(198, 61, 47, 0.38);
  color: #9e2a1e;
}

#objective-text {
  min-width: 0;
  line-height: 1.3;
}

#objective-action {
  appearance: none;
  border: none;
  background: var(--teal);
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Bottom Thumb-Friendly Sculpting Dock */
#hud-bottom {
  position: fixed;
  bottom: max(6px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  width: min(680px, calc(100vw - 12px));
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 7px 9px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 10;
}

/* Live Sand-to-Water Ratio Meter */
.meter-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.meter-track {
  position: relative;
  flex: 1;
  height: 12px;
  border-radius: 999px;
  /* 0-16% Dry Gold | 24-48% (w=12-24%) Golden Pack Zone | >70% (w>35%) Slurry Danger */
  background: linear-gradient(
    90deg,
    #f5dfb3 0%,
    #edd09a 18%,
    #c88d44 28%,
    #b8792d 46%,
    #966636 66%,
    #6d4724 82%,
    #3b7a8c 100%
  );
  border: 1px solid rgba(25, 45, 56, 0.20);
  overflow: visible;
}

.golden-band-marker {
  position: absolute;
  left: 24%;
  width: 24%;
  top: -2px;
  bottom: -2px;
  border: 1.5px solid rgba(184, 115, 20, 0.85);
  border-radius: 5px;
  pointer-events: none;
}

#meter-needle {
  position: absolute;
  top: -4px;
  left: 34%;
  width: 6px;
  height: 18px;
  border-radius: 3px;
  background: #ffffff;
  border: 2px solid var(--ink);
  transform: translateX(-50%);
  transition: left 0.08s linear;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.meter-labels {
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-shrink: 0;
}

#meter-readout {
  font-size: 11.5px;
  font-weight: 800;
  color: var(--ink);
  white-space: nowrap;
}

#meter-readout[data-tone="golden"] {
  color: #8f5607;
}

#meter-readout[data-tone="danger"] {
  color: var(--danger);
}

#meter-readout[data-tone="water"] {
  color: var(--teal);
}

#meter-bond {
  font-size: 10.5px;
  font-weight: 700;
  color: var(--ink-muted);
  white-space: nowrap;
}

/* Quick Ratio Toggle & Brush Size Row */
.sub-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.pour-presets {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.ratio-btn {
  appearance: none;
  border: 1px solid rgba(25, 45, 56, 0.15);
  background: rgba(255, 255, 255, 0.78);
  color: var(--ink-muted);
  font-size: 11.5px;
  font-weight: 700;
  padding: 4px 9px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}

#btn-pour-dry.is-active {
  background: #fbf0d7;
  border-color: #cda65e;
  color: #6b4e16;
}

#btn-pour-damp.is-active {
  background: #f8e4c1;
  border-color: #bd7615;
  color: #784604;
  box-shadow: inset 0 0 0 1px rgba(189, 118, 21, 0.25);
}

#brush-sizes {
  display: flex;
  align-items: center;
  gap: 3px;
  background: rgba(25, 45, 56, 0.06);
  padding: 2px;
  border-radius: 8px;
  flex-shrink: 0;
}

.mini-pill {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--ink-muted);
  font-size: 11px;
  font-weight: 800;
  width: 26px;
  height: 22px;
  border-radius: 6px;
  cursor: pointer;
}

.mini-pill.is-active {
  background: #ffffff;
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(25, 45, 56, 0.12);
}

/* Main 6-Tool Thumb Palette */
#tool-row {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 5px;
}

.tool-btn {
  appearance: none;
  border: 1px solid rgba(25, 45, 56, 0.12);
  background: rgba(255, 255, 255, 0.82);
  color: var(--ink);
  border-radius: 11px;
  padding: 5px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  min-height: 44px;
  transition: transform 0.08s ease, background 0.12s ease, border-color 0.12s ease;
}

.tool-btn:active {
  transform: scale(0.96);
}

.tool-btn.is-active {
  background: #ffffff;
  border-color: var(--teal);
  box-shadow: 0 0 0 1.5px rgba(13, 133, 153, 0.28);
  color: #085d6c;
}

.tool-icon {
  font-size: 16px;
  line-height: 1;
}

.tool-label {
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
}

/* Portrait Phone (390x844) Optimization */
@media (max-width: 600px) {
  .top-bar {
    padding: 4px 6px;
    gap: 4px;
  }
  .brand-mark {
    display: none;
  }
  #island-drawer { padding: 14px; }
  #mode-switcher { grid-template-columns: minmax(0, 1fr); }
  .island-option { min-height: 48px; }
  .env-actions {
    gap: 3px;
  }
  .action-chip {
    padding: 5px 6px;
    font-size: 11px;
  }
  .objective-bar {
    font-size: 11px;
    padding: 3px 10px;
  }
  #meter-bond {
    display: none;
  }
  .ratio-btn {
    font-size: 11px;
    padding: 4px 7px;
  }
}

/* Landscape Phone (844x390) Compact Single-Sightline Layout */
@media (max-height: 460px) {
  #hud-top {
    top: 4px;
    gap: 3px;
  }
  .top-bar {
    padding: 3px 7px;
    border-radius: 11px;
  }
  #island-drawer { padding: 10px 14px; }
  .drawer-head h2 { font-size: 23px; }
  .weather-controls { padding: 5px 0 8px; }
  .island-option { min-height: 48px; padding: 5px 10px; }
  .action-chip {
    padding: 3px 7px;
    font-size: 11px;
  }
  .objective-bar {
    padding: 2px 10px;
    font-size: 10.5px;
  }
  #hud-bottom {
    bottom: 4px;
    padding: 4px 8px;
    gap: 4px;
    border-radius: 12px;
    width: min(760px, calc(100vw - 12px));
  }
  .meter-track {
    height: 9px;
  }
  #meter-needle {
    height: 14px;
    top: -3px;
  }
  .tool-btn {
    flex-direction: row;
    min-height: 30px;
    padding: 3px 5px;
    gap: 4px;
  }
  .tool-icon {
    font-size: 13px;
  }
  .tool-label {
    font-size: 10.5px;
  }
}

#replay-dock {
  position: fixed;
  right: max(8px,env(safe-area-inset-right));
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  max-width: calc(100vw - 16px);
  pointer-events: none;
  font: 600 12px/1.2 system-ui,-apple-system,"Segoe UI",sans-serif;
  color: var(--ink,#192d38);
}

#replay-dock > * {
  pointer-events: auto;
}

#replay-bar {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: nowrap;
}

.rp-pill {
  appearance: none;
  border: 1px solid var(--card-border,rgba(27,48,59,.14));
  background: var(--card-bg,rgba(255,252,245,.91));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: var(--shadow,0 6px 20px rgba(24,46,58,.1));
  color: inherit;
  border-radius: 999px;
  padding: 7px 11px;
  font: inherit;
  cursor: pointer;
  white-space: nowrap;
  min-height: 32px;
}

.rp-pill:focus-visible,.rp-btn:focus-visible {
  outline: 2px solid var(--teal,#0d8599);
  outline-offset: 2px;
}

.rp-pill.is-cta {
  background: linear-gradient(180deg,#fff7e3,#ffefc9);
  border-color: rgba(189,118,21,.35);
}

#rp-rec .dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #c63d2f;
  margin-right: 5px;
  vertical-align: middle;
  animation: rpblink 1.6s ease-in-out infinite;
}

#rp-rec[data-state=replay] .dot {
  background: var(--teal,#0d8599);
  animation: none;
}

#rp-rec[data-state=full] .dot {
  background: #9aa6ad;
  animation: none;
}

@keyframes rpblink {
  50% {
    opacity: .35;
  }
}

@media (prefers-reduced-motion:reduce) {
  #rp-rec .dot {
    animation: none;
  }
}

#replay-drawer {
  width: min(340px,calc(100vw - 16px));
  max-height: var(--rp-max-h,60vh);
  overflow-y: auto;
  overscroll-behavior: contain;
  box-sizing: border-box;
  background: var(--card-bg,rgba(255,252,245,.94));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--card-border,rgba(27,48,59,.14));
  border-radius: 16px;
  box-shadow: var(--shadow,0 6px 20px rgba(24,46,58,.1));
  padding: 10px;
}

#replay-drawer[hidden] {
  display: none;
}

.rp-title {
  font-size: 12px;
  letter-spacing: .02em;
  color: var(--ink-muted,#516875);
  margin: 2px 2px 6px;
}

.rp-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.rp-btn {
  appearance: none;
  flex: 1 1 auto;
  border: 1px solid var(--card-border,rgba(27,48,59,.14));
  background: rgba(255,255,255,.75);
  color: inherit;
  border-radius: 10px;
  padding: 7px 9px;
  font: inherit;
  cursor: pointer;
  min-height: 32px;
  text-align: center;
}

.rp-btn.is-active {
  background: var(--teal-soft,rgba(13,133,153,.13));
  border-color: var(--teal,#0d8599);
  color: #0a5d6b;
}

.rp-btn:disabled {
  opacity: .45;
  cursor: default;
}

.rp-demo {
  display: block;
  width: 100%;
  text-align: left;
  margin-bottom: 6px;
}

.rp-demo small {
  display: block;
  font-weight: 500;
  color: var(--ink-muted,#516875);
  margin-top: 2px;
}

#rp-progress {
  height: 4px;
  border-radius: 2px;
  background: rgba(13,133,153,.15);
  overflow: hidden;
  margin: 2px 0 8px;
}

#rp-progress > i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--teal,#0d8599);
}

#rp-link {
  width: 100%;
  box-sizing: border-box;
  font: 500 11px ui-monospace,Menlo,monospace;
  border: 1px solid var(--card-border,rgba(27,48,59,.14));
  border-radius: 8px;
  padding: 6px;
  background: #fffdf8;
  color: inherit;
  margin-bottom: 8px;
}

#rp-link[hidden] {
  display: none;
}

#rp-toast {
  max-width: min(340px,calc(100vw - 16px));
  background: rgba(255,252,245,.96);
  border: 1px solid var(--card-border,rgba(27,48,59,.14));
  border-radius: 12px;
  padding: 7px 10px;
  box-shadow: var(--shadow,0 6px 20px rgba(24,46,58,.1));
  font-weight: 600;
}

#rp-toast[hidden] {
  display: none;
}

/* Keep both drawer triggers reachable above the sculpting palette. */
#island-drawer[open] {
  z-index: 25;
  top: var(--island-top, 82px);
  max-height: calc(100dvh - var(--island-top, 82px) - var(--island-bottom, 160px));
}
#rp-toast {
  position: absolute;
  right: 0;
  bottom: calc(100% + 5px);
  width: max-content;
  pointer-events: none;
}
#replay-drawer #rp-toast {
  position: static;
  width: auto;
  margin-bottom: 6px;
}

a.brand-mark { text-decoration: none; color: inherit; }
a.brand-mark:hover { text-decoration: underline; }
