:root {
  --bg: #061618;
  --panel: rgba(7, 28, 31, 0.92);
  --line: rgba(255, 255, 255, 0.16);
  --text: #f8fbfb;
  --muted: #bfd2d3;
  --accent: #0c676d;
  --accent2: #f2a01a;
  --shadow: rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: var(--bg); color: var(--text); font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, .fileButton {
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.08);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
button:active, .fileButton:active { transform: translateY(1px); }
button.primary { background: linear-gradient(135deg, var(--accent), var(--accent2)); border: none; color: #fff; box-shadow: 0 8px 24px rgba(242,160,26,.28); }
.secondaryBtn { width: 100%; margin-top: 8px; }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }
#topPanel { padding: 12px; background: linear-gradient(180deg, rgba(6,22,24,.98), rgba(6,22,24,.86)); border-bottom: 1px solid rgba(242,160,26,.22); z-index: 10; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.brandLogo { width: clamp(120px, 22vw, 210px); height: auto; object-fit: contain; filter: drop-shadow(0 6px 16px rgba(0,0,0,.25)); }
h1 { font-size: 18px; margin: 0; line-height: 1.15; color: #ffffff; }
p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
#fileInput, #hiddenCalibration { display: none; }

#viewerWrap { position: relative; flex: 1; min-height: 0; }
#viewerCanvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; background: radial-gradient(circle at 50% 15%, #16363a 0%, #071416 48%, #041012 100%); }
#loading { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: rgba(0,0,0,.58); color: #fff; padding: 12px 16px; border-radius: 12px; font-weight: 650; pointer-events: none; max-width: 90%; text-align: center; z-index: 5; }
#statusPanel { min-height: 70px; max-height: 170px; overflow: auto; background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px; font-size: 13px; color: var(--muted); z-index: 10; }
#debugLog { margin: 6px 0 0; white-space: pre-wrap; font-size: 11px; line-height: 1.35; color: #d6e3e4; max-height: 100px; overflow: auto; }

#controlDock {
  position: absolute; right: 10px; top: 10px; z-index: 26;
  display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end;
  max-width: min(520px, calc(100% - 20px)); padding: 6px;
  border: 1px solid rgba(255,255,255,.14); border-radius: 14px;
  background: rgba(6, 22, 24, .68); backdrop-filter: blur(8px);
}
#controlDock button, #controlDock .fileButton { padding: 8px 10px; font-size: 12px; border-radius: 10px; }

.toolPanel {
  position: absolute; z-index: 24; min-width: 250px; max-width: min(420px, calc(100% - 24px));
  background: rgba(5, 20, 22, 0.80); border: 1px solid rgba(242,160,26,0.24);
  border-radius: 14px; padding: 12px; box-shadow: 0 8px 24px var(--shadow); backdrop-filter: blur(8px);
}
#movePanel { right: 12px; bottom: 12px; }
#sceneSettingsPanel { left: 12px; bottom: 12px; min-width: 240px; }
.panelTitle { font-size: 13px; font-weight: 800; margin-bottom: 8px; color: #ffffff; }
.panelHint { margin-top: 8px; color: var(--muted); font-size: 11px; }
.moveGrid { display: grid; gap: 8px; }
.axisRow { display: grid; grid-template-columns: 44px 1fr 1fr; gap: 8px; align-items: center; }
.axisRow span { color: #ffe6bf; font-weight: 800; }
.axisRow button { padding: 8px 10px; }

#vrTopControls { display: none; position: absolute; left: 50%; top: 12px; transform: translateX(-50%); z-index: 20; gap: 10px; }
#vrDivider { display: none; position: absolute; left: 50%; top: 0; bottom: 0; width: 2px; transform: translateX(-1px); background: rgba(255,255,255,.8); z-index: 18; pointer-events: none; }
#vrLensOverlay { display: none; position: absolute; inset: 0; z-index: 17; pointer-events: none; }
#vrLensOverlay .lens { position: absolute; top: 4%; width: 43%; height: 92%; border-radius: 16% / 11%; border: 1px solid rgba(255,255,255,.16); background: rgba(255,255,255,.01); }
#vrLensOverlay .left { left: 4%; }
#vrLensOverlay .right { right: 4%; }

#exitFullscreenBtn {
  display: none; position: absolute; left: 50%; top: 12px; transform: translateX(-50%); z-index: 40;
  background: rgba(0,0,0,.65); border: 1px solid rgba(255,255,255,.25);
}

body.vrMode #topPanel, body.vrMode #statusPanel { display: none; }
body.vrMode #vrTopControls { display: flex; }
body.vrMode #vrDivider { display: block; }
body.vrMode #vrLensOverlay { display: block; }
body.vrMode #loading { top: 80%; }
body.vrMode #viewerCanvas { background: #000; }
body.vrMode #vrTopControls button { padding: 8px 12px; font-size: 13px; background: rgba(0,0,0,.55); }

body.vrMode #controlDock,
body.vrMode #movePanel,
body.vrMode #sceneSettingsPanel,
body.fullscreenMode #controlDock,
body.fullscreenMode #movePanel,
body.fullscreenMode #sceneSettingsPanel,
body.fullscreenMode #topPanel,
body.fullscreenMode #statusPanel { display: none !important; }
body.fullscreenMode:not(.vrMode) #exitFullscreenBtn { display: block; }
body.vrMode #exitFullscreenBtn { display: none !important; }

@media (max-width: 760px) {
  #topPanel { padding: 10px; }
  .brand { margin-bottom: 8px; flex-direction: column; align-items: flex-start; }
  .brandLogo { width: min(220px, 62vw); }
  h1 { font-size: 16px; }
  p { font-size: 11px; }
  button, .fileButton { padding: 9px 10px; font-size: 13px; border-radius: 10px; }
  #controlDock { left: 8px; right: 8px; top: 8px; justify-content: center; }
  #controlDock button, #controlDock .fileButton { padding: 8px 9px; font-size: 12px; }
  .toolPanel { min-width: unset; max-width: calc(100% - 16px); }
  #movePanel { left: 8px; right: 8px; bottom: 8px; }
  #sceneSettingsPanel { left: 8px; right: 8px; bottom: 190px; }
}

@media (orientation: landscape) and (max-height: 520px) {
  #controlDock { top: 8px; right: 8px; left: auto; max-width: 64vw; justify-content: flex-end; }
  body:not(.vrMode) #topPanel { max-height: 46vh; overflow: auto; }
  #movePanel { bottom: 8px; max-width: 340px; }
  #sceneSettingsPanel { bottom: 8px; left: 8px; max-width: 280px; }
}

.axisValueRow { grid-template-columns: 44px 1fr 96px 1fr; }
.axisValueRow input { width: 100%; min-width: 0; border: 1px solid rgba(255,255,255,.16); background: rgba(255,255,255,.08); color: var(--text); border-radius: 10px; padding: 8px 10px; font-size: 13px; font-weight: 700; text-align: center; }
.axisValueRow input:focus { outline: 2px solid rgba(242,160,26,.45); outline-offset: 1px; }
@media (max-width: 760px) { .axisValueRow { grid-template-columns: 40px 1fr 86px 1fr; } .axisValueRow input { font-size: 12px; padding: 8px 6px; } }

/* v19 auth + saved files */
#accountBar { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 6px 0 10px; }
#guestLinks, #userLinks { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.navButton { display: inline-block; text-decoration: none; border: 1px solid var(--line); background: rgba(255,255,255,.08); color: var(--text); padding: 8px 12px; border-radius: 12px; font-size: 13px; font-weight: 700; }
.primaryLink { background: linear-gradient(135deg, var(--accent), var(--accent2)); border: none; }
#welcomeText { color: #ffe6bf; font-size: 13px; font-weight: 700; }
#savedPanel { margin-top: 8px; padding: 10px; border: 1px solid rgba(242,160,26,.2); border-radius: 14px; background: rgba(255,255,255,.04); }
.inlineTitle { margin-bottom: 8px; }
#savedList { display: flex; flex-wrap: wrap; gap: 6px; }
.savedRow { display: flex; align-items: center; gap: 6px; max-width: 100%; padding: 6px; border-radius: 10px; background: rgba(0,0,0,.20); }
.savedRow span { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: var(--text); }
.savedRow button { padding: 6px 8px; font-size: 12px; border-radius: 8px; }
.savedEmpty { color: var(--muted); font-size: 12px; }

.authPage { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(circle at 50% 0%, #17464b 0%, #061618 56%); padding: 20px; }
.authCard { width: min(440px, 100%); border: 1px solid rgba(242,160,26,.24); border-radius: 22px; background: rgba(6,22,24,.88); padding: 22px; box-shadow: 0 12px 40px rgba(0,0,0,.35); }
.authLogo { width: min(240px, 70%); display: block; margin: 0 auto 12px; }
.authCard h1 { text-align: center; margin-bottom: 12px; }
.authCard label { display: block; margin: 10px 0 5px; font-size: 13px; color: #ffe6bf; font-weight: 700; }
.authCard input, .authCard select { width: 100%; padding: 11px 12px; border-radius: 12px; border: 1px solid rgba(255,255,255,.16); background: rgba(255,255,255,.08); color: var(--text); }
.authCard button { width: 100%; margin-top: 14px; }
.authLinks { text-align: center; margin-top: 14px; color: var(--muted); font-size: 13px; }
.authLinks a { color: #ffbf5d; }
.errorBox { background: rgba(255, 70, 70, .12); border: 1px solid rgba(255, 70, 70, .35); color: #ffd5d5; padding: 10px; border-radius: 12px; margin: 8px 0; }
.successBox { background: rgba(58, 200, 120, .12); border: 1px solid rgba(58, 200, 120, .35); color: #d9ffe7; padding: 10px; border-radius: 12px; margin: 8px 0; }

.adminPage { min-height: 100vh; background: #061618; color: var(--text); padding: 18px; overflow: auto; }
.adminHeader { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.adminHeader img { width: 180px; }
.adminGrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; }
.adminCard { border: 1px solid rgba(242,160,26,.22); border-radius: 18px; background: rgba(255,255,255,.04); padding: 14px; overflow: auto; }
.adminCard h2 { margin: 0 0 10px; font-size: 18px; }
.adminTable { width: 100%; border-collapse: collapse; font-size: 13px; }
.adminTable th, .adminTable td { border-bottom: 1px solid rgba(255,255,255,.10); padding: 8px; text-align: left; vertical-align: top; }
.adminTable input, .adminTable select { padding: 6px; border-radius: 8px; border: 1px solid rgba(255,255,255,.18); background: rgba(255,255,255,.08); color: var(--text); max-width: 120px; }
.adminTable button { padding: 6px 8px; border-radius: 8px; font-size: 12px; margin: 2px; }
@media (max-width: 760px) { #savedList { flex-direction: column; } .savedRow span { max-width: 140px; } }

/* v20 professional student workspace */
.studentTop { display: grid; grid-template-columns: 1fr auto; gap: 10px 18px; align-items: start; }
.studentTop .brand { margin-bottom: 0; }
.studentTop .controls { grid-column: 1 / -1; }
#accountBar { justify-content: flex-end; margin: 0; }
.loggedIn #guestLinks { display: none !important; }
#userLinks { justify-content: flex-end; }
#logoutBtn { background: rgba(242,160,26,.14); border-color: rgba(242,160,26,.34); }
.compactControls { padding-top: 4px; }

.projectHome {
  position: absolute;
  inset: 0;
  z-index: 35;
  padding: 22px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 16px;
  overflow: auto;
  background:
    radial-gradient(circle at 20% 0%, rgba(242,160,26,.18), transparent 28%),
    radial-gradient(circle at 85% 10%, rgba(12,103,109,.30), transparent 34%),
    rgba(4, 16, 18, .90);
  backdrop-filter: blur(10px);
}
.projectHeroCard, .savedProjectsCard {
  border: 1px solid rgba(242,160,26,.24);
  background: rgba(255,255,255,.065);
  border-radius: 24px;
  box-shadow: 0 18px 55px rgba(0,0,0,.32);
}
.projectHeroCard {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 210px;
  align-items: center;
  gap: 18px;
  padding: 22px;
}
.projectHeroText h2 { margin: 8px 0 6px; font-size: clamp(24px, 4vw, 42px); line-height: 1.05; }
.projectHeroText p { font-size: 15px; max-width: 560px; color: #d7e8e9; }
.badge { display: inline-flex; align-items: center; gap: 6px; border: 1px solid rgba(242,160,26,.28); background: rgba(242,160,26,.12); color: #ffe7bd; padding: 7px 12px; border-radius: 999px; font-size: 12px; font-weight: 800; }
.projectActions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
.bigAction { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; min-height: 46px; border-radius: 16px; border: 1px solid var(--line); color: var(--text); background: rgba(255,255,255,.08); padding: 12px 18px; font-weight: 850; }
.bigAction.primary { background: linear-gradient(135deg, var(--accent), var(--accent2)); border: none; }
.projectLimitCard { justify-self: end; width: 190px; aspect-ratio: 1; border-radius: 26px; display: grid; place-items: center; text-align: center; background: linear-gradient(160deg, rgba(12,103,109,.58), rgba(242,160,26,.25)); border: 1px solid rgba(255,255,255,.14); }
.projectLimitCard span { font-size: 54px; font-weight: 950; line-height: .85; color: #fff; }
.projectLimitCard p { margin: 0; font-weight: 800; color: #ffe9c6; }
.projectLimitCard small { color: #e4f1f1; }
.savedProjectsCard { padding: 18px; min-height: 260px; }
.savedHeader { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.savedHeader h3 { margin: 0; font-size: 22px; }
.projectList { display: grid; gap: 10px; }
.projectItem { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 14px; border: 1px solid rgba(255,255,255,.12); border-radius: 16px; background: rgba(0,0,0,.18); }
.projectInfo { display: grid; gap: 3px; min-width: 0; }
.projectInfo b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.projectInfo span { color: var(--muted); font-size: 12px; }
.projectItemActions { display: flex; gap: 8px; }
.projectItemActions button, .primaryMini, .dangerMini { padding: 8px 11px; font-size: 12px; border-radius: 10px; }
.primaryMini { background: rgba(12,103,109,.55); }
.dangerMini, .dangerBtn { background: rgba(255,80,80,.12); border-color: rgba(255,80,80,.34); color: #ffd5d5; }
.emptyProjects { display: grid; gap: 5px; padding: 22px; border-radius: 18px; border: 1px dashed rgba(255,255,255,.22); color: #eefafa; background: rgba(255,255,255,.04); }
.emptyProjects span { color: var(--muted); font-size: 13px; }
body.projectHomeOpen #controlDock, body.projectHomeOpen #movePanel { display: none !important; }

.authSubtitle { text-align: center; margin: -2px 0 12px; font-size: 13px; color: var(--muted); }
.studentAuth { background: linear-gradient(180deg, rgba(7,28,31,.95), rgba(5,20,22,.88)); }
.proPage { background: radial-gradient(circle at 0% 0%, rgba(242,160,26,.15), transparent 30%), #061618; }
.adminShell { max-width: 1280px; margin: 0 auto; }
.proHeader { border: 1px solid rgba(242,160,26,.2); border-radius: 22px; padding: 14px; background: rgba(255,255,255,.04); }
.headerBrand { display: flex; align-items: center; gap: 14px; }
.headerBrand h1 { font-size: 26px; }
.headerBrand p { font-size: 13px; }
.statsGrid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px; margin-bottom: 14px; }
.statCard { border: 1px solid rgba(242,160,26,.18); background: rgba(255,255,255,.05); border-radius: 18px; padding: 14px; display: grid; gap: 4px; }
.statCard b { font-size: 30px; color: #fff; }
.statCard span { color: var(--muted); font-size: 12px; }
.wideCard { grid-column: 1 / -1; }
.stackForm { display: grid; gap: 8px; }
.stackForm label { color: #ffe6bf; font-size: 13px; font-weight: 800; }
.stackForm input, .stackForm select { width: 100%; padding: 11px 12px; border-radius: 12px; border: 1px solid rgba(255,255,255,.16); background: rgba(255,255,255,.08); color: var(--text); }
.tableWrap { overflow: auto; }
.roleBadge { display: inline-block; padding: 5px 9px; border-radius: 999px; font-size: 12px; font-weight: 900; background: rgba(255,255,255,.1); }
.roleBadge.admin { color: #ffe7b6; background: rgba(242,160,26,.16); }
.roleBadge.user { color: #d5fbff; background: rgba(12,103,109,.30); }

@media (max-width: 860px) {
  .studentTop { grid-template-columns: 1fr; }
  #accountBar { justify-content: flex-start; }
  .projectHome { padding: 12px; }
  .projectHeroCard { grid-template-columns: 1fr; }
  .projectLimitCard { justify-self: stretch; width: auto; aspect-ratio: auto; padding: 18px; }
  .projectItem { grid-template-columns: 1fr; }
  .projectItemActions { justify-content: flex-start; flex-wrap: wrap; }
  .statsGrid { grid-template-columns: repeat(2, minmax(0,1fr)); }
  .headerBrand { align-items: flex-start; flex-direction: column; }
}

/* v21 fixes */
.adminStatsThree { grid-template-columns: repeat(3, minmax(0,1fr)); }
@media (max-width: 860px) { .adminStatsThree { grid-template-columns: 1fr; } }

/* v22: force project workspace to really hide when opening/new project */
.projectHome[hidden] { display: none !important; opacity: 0 !important; pointer-events: none !important; }
body:not(.projectHomeOpen) .projectHome { display: none !important; }
body.projectHomeOpen .projectHome:not([hidden]) { display: grid !important; }
body:not(.projectHomeOpen) #controlDock, body:not(.projectHomeOpen) #movePanel { display: flex; }
body:not(.projectHomeOpen) #movePanel { display: block; }

.colorGrid { display: grid; gap: 10px; }
.colorGrid label { display: grid; grid-template-columns: 1fr 56px; align-items: center; gap: 10px; color: #ffe6bf; font-size: 12px; font-weight: 800; }
.colorGrid input[type="color"] { width: 56px; height: 38px; border: 1px solid rgba(255,255,255,.20); border-radius: 10px; background: rgba(255,255,255,.08); padding: 3px; cursor: pointer; }
body.projectHomeOpen #sceneSettingsPanel { display: none !important; }
body:not(.projectHomeOpen) #sceneSettingsPanel { display: block; }
