:root {
  --bg: #0e0f11;
  --panel: #15171a;
  --text: #e9f1f1;
  --muted: #9aa3a8;
  --accent: #17c3b2;
  --accent-2: #ffb703;
  --chip: #22252a;
  --barH: 56px;
  --barW: 56px;
  --titleH: 48px;
  --ctrlH: 148px;
}
* { box-sizing: border-box; }
html, body, #app { height: 100%; margin: 0; background: var(--bg); color: var(--text); font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Noto Sans, Ubuntu, Cantarell; }

/* Utility */
.hidden { display: none !important; }

/* Title bar */
#titlebar {
  height: var(--titleH);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  padding-right: calc(8px + env(safe-area-inset-right, 0px) + 84px);
  border-bottom: 1px solid rgba(255,255,255,0.06);
  background: #0f1115;
}
#titlebar.expanded { height: auto; align-items: flex-start; padding-bottom: 8px; }
#titlebar .title { font-weight: 700; letter-spacing: 0.2px; }
#titlebar .title-actions { position: relative; }

/* Help dropdown */
#help-dropdown.hidden { display: none; }
#help-dropdown {
  position: absolute;
  right: 0; top: calc(100% + 6px);
  width: min(84vw, 420px);
  max-height: 60vh;
  overflow: auto;
  background: #0f1216;
  border: 1px solid #2a2e34;
  border-radius: 10px;
  padding: 10px;
  z-index: 60;
}
#titlebar.expanded #help-dropdown {
  position: static; width: 100%; max-height: 40vh; margin-top: 6px; z-index: auto;
}

/* Tutorial dropdown mirrors help dropdown */
#tutorial-dropdown.hidden { display: none; }
#tutorial-dropdown {
  position: absolute;
  right: 0; top: calc(100% + 6px);
  width: min(84vw, 420px);
  max-height: 60vh;
  overflow: auto;
  background: #0f1216;
  border: 1px solid #2a2e34;
  border-radius: 10px;
  padding: 10px;
  z-index: 60;
}
#titlebar.expanded #tutorial-dropdown {
  position: static; width: 100%; max-height: 40vh; margin-top: 6px; z-index: auto;
}

/* Theory dropdown mirrors help/tutorial */
#theory-dropdown.hidden { display: none; }
#theory-dropdown {
  position: absolute;
  right: 0; top: calc(100% + 6px);
  width: min(84vw, 420px);
  max-height: 60vh;
  overflow: auto;
  background: #0f1216;
  border: 1px solid #2a2e34;
  border-radius: 10px;
  padding: 10px;
  z-index: 60;
}
#titlebar.expanded #theory-dropdown {
  position: static; width: 100%; max-height: 40vh; margin-top: 6px; z-index: auto;
}

/* Control bar */
#controlbar {
  /* auto-height so it never overlaps content below */
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: min-content;
  align-content: start;
  gap: 6px;
  padding: 6px 8px;
  background: #0f1115;
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

/* New 3x3 layout with square center (now below title+controls) */
#app { display: flex; flex-direction: column; }
#layout {
  /* fill remaining space after auto-height bars */
  flex: 1;
  min-height: 0; /* allow inner grid to size without overflow */
  width: 100%;
  display: grid;
  grid-template-rows: var(--barH) 1fr var(--barH);
  grid-template-columns: var(--barW) 1fr var(--barW);
  grid-template-areas:
    "top top top"
    "left center right"
    "bottom bottom bottom";
  gap: 6px;
  padding: 6px;
}
.bar.top { grid-area: top; display: flex; gap: 6px; align-items: center; justify-content: center; flex-wrap: wrap; }
.bar.bottom { grid-area: bottom; display: flex; gap: 6px; align-items: center; justify-content: center; flex-wrap: wrap; }
.bar.left { grid-area: left; display: flex; flex-direction: column; gap: 6px; align-items: stretch; justify-content: center; }
.bar.right { grid-area: right; display: flex; flex-direction: column; gap: 6px; align-items: stretch; justify-content: center; }

.menu-btn {
  background: var(--chip);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 10px;
  padding: 8px 10px;
  min-width: 44px; min-height: 36px;
  font-size: 13px;
  /* 3D glassy green tint */
  background:
    linear-gradient(180deg, rgba(23,195,178,0.20), rgba(23,195,178,0.10)),
    linear-gradient(180deg, rgba(255,255,255,0.18), rgba(255,255,255,0.06));
  border: 1px solid rgba(23,195,178,0.35);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.15) inset,
    0 -1px 0 rgba(0,0,0,0.35) inset,
    0 6px 14px rgba(0,0,0,0.35),
    0 2px 6px rgba(23,195,178,0.25);
  backdrop-filter: blur(2px);
  transition: transform 120ms ease, box-shadow 120ms ease, filter 120ms ease;
}
.menu-btn.vertical {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  padding: 10px 8px;
}
/* Hover/Active depth for menu buttons */
.menu-btn:hover {
  box-shadow:
    0 1px 0 rgba(255,255,255,0.18) inset,
    0 -1px 0 rgba(0,0,0,0.4) inset,
    0 10px 20px rgba(0,0,0,0.45),
    0 3px 10px rgba(23,195,178,0.35);
  filter: brightness(1.05);
}
.menu-btn:active {
  transform: translateY(1px);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.12) inset,
    0 -1px 0 rgba(0,0,0,0.5) inset,
    0 4px 10px rgba(0,0,0,0.35),
    0 2px 6px rgba(23,195,178,0.30);
}

/* Center square surface */
#center { grid-area: center; display: grid; place-items: center; }
#surface-wrapper {
  /* fill the center cell and center the square surface inside */
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
}
#surface {
  position: relative;
  background: var(--panel);
  border-radius: 14px;
  overflow: hidden;
  /* maintain a perfect square that fits within the center cell */
  height: 100%;
  width: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
}
/* Modern high-quality 3D raised green frame around the grid */
#surface::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 16px;
  pointer-events: none;

  /* Create a thick ring by masking out the center */
  padding: 14px; /* frame thickness */
  -webkit-mask:
    linear-gradient(#000 0 0) content-box,
    linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;

  /* 3D beveled rim: layered gradient for tint + subtle metallic sheen */
  background:
    linear-gradient(160deg,
      rgba(23,195,178,0.25) 0%,
      rgba(23,195,178,0.10) 35%,
      rgba(23,195,178,0.06) 60%,
      rgba(0,0,0,0.0) 100%
    ),
    radial-gradient(120% 100% at 20% 0%,
      rgba(255,255,255,0.20) 0%,
      rgba(255,255,255,0.05) 35%,
      rgba(23,195,178,0.10) 60%,
      rgba(23,195,178,0.0) 100%
    );

  /* Multi-stage shadows for raised appearance and bevel */
  box-shadow:
    /* crisp edge */
    0 0 0 2px rgba(23,195,178,0.95),
    /* outer lift */
    0 14px 26px rgba(0,0,0,0.40),
    0 4px 10px rgba(0,0,0,0.35),
    /* inner bevel highlights */
    inset 0 2px 3px rgba(255,255,255,0.12),
    inset 0 -6px 12px rgba(23,195,178,0.22);
  z-index: 2;
}

#surface::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 16px;
  pointer-events: none;

  /* thin inner lip highlight for premium finish */
  padding: 2px;
  -webkit-mask:
    linear-gradient(#000 0 0) content-box,
    linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;

  background:
    linear-gradient(180deg,
      rgba(255,255,255,0.22) 0%,
      rgba(255,255,255,0.06) 22%,
      rgba(23,195,178,0.10) 60%,
      rgba(23,195,178,0.0) 100%
    );

  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.18),
    inset 0 -2px 6px rgba(23,195,178,0.18);
  z-index: 3;
}
/* Canvas remains full-size under the frame */
#grid { width: 100%; height: 100%; display: block; }

/* Modal */
#modal.hidden { display: none; }
#modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: grid; place-items: center;
  z-index: 50;
}
.modal-card {
  width: min(720px, calc(100vw - 24px));
  max-height: min(80vh, 640px);
  background: #0f1216;
  border: 1px solid #2a2e34;
  border-radius: 12px;
  overflow: hidden;
  display: grid; grid-template-rows: auto 1fr;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; background: #14171c; border-bottom: 1px solid rgba(255,255,255,0.06);
}
#modal-body { padding: 10px; overflow: auto; }

/* Reuse existing UI styles */
button, .chip, a.secondary {
  background: var(--chip); color: var(--text); border: none; border-radius: 10px; padding: 10px 12px;
  font-size: 14px; min-width: 44px; min-height: 36px;
  /* 3D glassy green tint */
  background:
    linear-gradient(180deg, rgba(23,195,178,0.22), rgba(23,195,178,0.10)),
    linear-gradient(180deg, rgba(255,255,255,0.18), rgba(255,255,255,0.06));
  border: 1px solid rgba(23,195,178,0.35);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.15) inset,
    0 -1px 0 rgba(0,0,0,0.35) inset,
    0 6px 14px rgba(0,0,0,0.35),
    0 2px 6px rgba(23,195,178,0.25);
  backdrop-filter: blur(2px);
  transition: transform 120ms ease, box-shadow 120ms ease, filter 120ms ease;
}
button.primary { background: var(--accent); color: #082b2a; font-weight: 700; 
  /* keep primary emphasis within glassy style */
  background:
    linear-gradient(180deg, rgba(23,195,178,0.28), rgba(23,195,178,0.14)),
    linear-gradient(180deg, rgba(255,255,255,0.20), rgba(255,255,255,0.08));
}
button.secondary, a.secondary { background: #1b1e23; 
  background:
    linear-gradient(180deg, rgba(23,195,178,0.18), rgba(23,195,178,0.08)),
    linear-gradient(180deg, rgba(255,255,255,0.16), rgba(255,255,255,0.05));
}
button:disabled, .chip:disabled { opacity: 0.5; }
a.secondary { text-decoration: none; display: inline-flex; align-items: center; }
/* Hover/Active depth for general buttons */
button:hover, .chip:hover, a.secondary:hover {
  box-shadow:
    0 1px 0 rgba(255,255,255,0.18) inset,
    0 -1px 0 rgba(0,0,0,0.4) inset,
    0 10px 20px rgba(0,0,0,0.45),
    0 3px 10px rgba(23,195,178,0.35);
  filter: brightness(1.05);
}
/* Active State for Toggles */
.chip.active, button.active {
  background: var(--accent);
  color: #082b2a;
  box-shadow: 0 0 10px rgba(23,195,178,0.4);
  border-color: var(--accent);
  font-weight: 700;
}

button:active, .chip:active, a.secondary:active {
  transform: translateY(1px);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.12) inset,
    0 -1px 0 rgba(0,0,0,0.5) inset,
    0 4px 10px rgba(0,0,0,0.35),
    0 2px 6px rgba(23,195,178,0.30);
}

.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.sliders { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.slider { display: grid; gap: 6px; align-items: center; }
.slider label { 
  display: flex; 
  justify-content: space-between;
  font-size: 12px; 
  color: var(--muted); 
}
.slider input[type="range"] { width: 100%; accent-color: var(--accent-2); }

#cursor {
  position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--accent); background: rgba(23,195,178,0.1); transform: translate(-9px, -9px); pointer-events: none;
  box-shadow: 0 0 12px rgba(23,195,178,0.6);
  z-index: 4; /* ensure it stays sharply visible above canvas and frame overlays */
  will-change: transform; /* keep motion crisp */
}
#legend {
  position: absolute; right: 8px; top: 8px; background: rgba(0,0,0,0.35); backdrop-filter: blur(6px);
  padding: 8px 10px; border-radius: 8px; font-size: 12px; color: var(--muted);
}

/* Hidden panel content containers */
.panel-content.hidden { display: none; }

/* Legacy help floater retained for notices (now feeds dropdown) */
#help { position: fixed; right: 8px; top: calc(var(--titleH) + 24px); background: rgba(0,0,0,0.7); backdrop-filter: blur(8px); border: 1px solid #2a2e34; border-radius: 12px; padding: 12px; max-width: calc(100% - 16px); max-height: 60vh; overflow: auto; font-size: 13px; }
#help h3 { margin: 6px 0 8px; font-size: 14px; }
#help ul { margin: 0; padding-left: 16px; }
#help p { margin: 6px 0; color: var(--muted); }

/* no style changes needed for Z input; it inherits chip/button styling */
a { color: var(--text); }
a:visited { color: var(--text); }