/* =========================================================
   AI Hub WebGL UI — Token-based theming (easy to change)
   Theme switch:
     document.documentElement.dataset.theme = "dark";
     dark | light | ocean | violet | sunset
   ========================================================= */

/* ---------- Base Theme Tokens (DEFAULT = dark) ---------- */
:root {
  /* Core hues (HSL) — tweak these to reshape the whole UI fast */
  --h-bg: 220;
  --h-accent: 205; /* speaking / highlights */
  --h-danger: 0; /* recording */

  --accent-s: 92%;
  --accent-l: 60%;

  --danger-s: 80%;
  --danger-l: 55%;

  /* Neutrals */
  --bg-l: 6%;
  --panel-l: 10%;
  --text-l: 96%;

  /* Semantic colors (use these everywhere) */
  --bg: hsl(var(--h-bg) 25% var(--bg-l));
  --text: hsl(var(--h-bg) 18% var(--text-l));
  --text-muted: hsl(var(--h-bg) 14% var(--text-l) / 0.82);

  --panel: hsl(var(--h-bg) 18% var(--panel-l) / 0.72);
  --panel-strong: hsl(var(--h-bg) 18% var(--panel-l) / 0.9);

  --border: hsl(var(--h-bg) 18% 95% / 0.12);
  --border-soft: hsl(var(--h-bg) 18% 95% / 0.1);
  --border-strong: hsl(var(--h-bg) 18% 95% / 0.18);

  --btn-bg: hsl(var(--h-bg) 10% 14% / 0.55);
  --btn-bg-hover: hsl(var(--h-bg) 10% 14% / 0.75);
  --btn-text: var(--text);

  --accent: hsl(var(--h-accent) var(--accent-s) var(--accent-l));
  --danger: hsl(var(--h-danger) var(--danger-s) var(--danger-l));

  /* Unity loading overlay */
  --unity-bg: hsl(var(--h-bg) 25% 0%);
  --unity-overlay: hsl(var(--h-bg) 25% 0% / 0.65);
  --progress-track: hsl(var(--h-bg) 8% 14%);
  --progress-border: hsl(var(--h-bg) 8% 20%);
  --progress-fill: hsl(var(--h-bg) 15% 92%);

  /* Message bubbles */
  --msg-bg: hsl(var(--h-bg) 25% 0% / 0.35);
  --msg-border: hsl(var(--h-bg) 18% 95% / 0.08);
  --msg-user-bg: hsl(var(--h-bg) 10% 95% / 0.08);

  /* Mic states */
  --mic-idle-bg: hsl(var(--h-bg) 10% 14% / 0.6);
  --mic-neutral-bg: hsl(var(--h-bg) 0% 32% / 0.6);
  --mic-speaking-bg: hsl(var(--h-accent) var(--accent-s) var(--accent-l) / 0.6);
  --mic-recording-bg: hsl(
    var(--h-danger) var(--danger-s) var(--danger-l) / 0.65
  );

  /* Shadows / glass */
  --shadow-strong: 0 10px 30px hsl(0 0% 0% / 0.35);
  --shadow-soft: 0 10px 30px hsl(0 0% 0% / 0.25);
  --blur: blur(10px);

  /* Legacy compatibility */
  --line: var(--border);
}

/* ---------- Theme Overrides ---------- */
:root[data-theme="light"] {
  --h-bg: 220;
  --h-accent: 210;
  --accent-s: 90%;
  --accent-l: 45%;

  --bg-l: 97%;
  --panel-l: 100%;
  --text-l: 12%;

  --bg: hsl(var(--h-bg) 25% var(--bg-l));
  --text: hsl(var(--h-bg) 18% var(--text-l));
  --text-muted: hsl(var(--h-bg) 16% 25% / 0.7);

  --panel: hsl(var(--h-bg) 25% 100% / 0.82);
  --panel-strong: hsl(var(--h-bg) 25% 100% / 0.95);

  --border: hsl(var(--h-bg) 18% 10% / 0.1);
  --border-soft: hsl(var(--h-bg) 18% 10% / 0.08);
  --border-strong: hsl(var(--h-bg) 18% 10% / 0.14);

  --btn-bg: hsl(var(--h-bg) 20% 92% / 0.9);
  --btn-bg-hover: hsl(var(--h-bg) 20% 88% / 0.95);
  --btn-text: var(--text);

  --unity-bg: hsl(var(--h-bg) 25% 0%);
  --unity-overlay: hsl(0 0% 0% / 0.55);
  --progress-track: hsl(var(--h-bg) 10% 88%);
  --progress-border: hsl(var(--h-bg) 10% 78%);
  --progress-fill: hsl(var(--h-bg) 10% 20%);

  --msg-bg: hsl(var(--h-bg) 20% 92% / 0.55);
  --msg-border: hsl(var(--h-bg) 18% 10% / 0.1);
  --msg-user-bg: hsl(var(--h-accent) 70% 55% / 0.18);

  --mic-idle-bg: hsl(var(--h-bg) 20% 92% / 0.95);
  --mic-neutral-bg: hsl(var(--h-bg) 0% 75% / 0.95);
  --shadow-strong: 0 10px 30px hsl(0 0% 0% / 0.18);
  --shadow-soft: 0 10px 30px hsl(0 0% 0% / 0.12);
}

:root[data-theme="ocean"] {
  --h-bg: 215;
  --h-accent: 190; /* teal/cyan */
  --accent-s: 95%;
  --accent-l: 58%;
  --bg-l: 7%;
  --panel-l: 11%;
}

:root[data-theme="violet"] {
  --h-bg: 255;
  --h-accent: 285; /* violet/pink */
  --accent-s: 92%;
  --accent-l: 62%;
  --bg-l: 7%;
  --panel-l: 12%;
}

:root[data-theme="sunset"] {
  --h-bg: 230;
  --h-accent: 18; /* warm orange */
  --accent-s: 95%;
  --accent-l: 58%;
  --bg-l: 7%;
  --panel-l: 12%;
}

/* =========================================================
   Global
   ========================================================= */
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ===== Unity Fullscreen ===== */
#unity-wrap {
  position: fixed;
  inset: 0;
  background: var(--unity-bg);
  overflow: hidden;
}

#unity-stage {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: stretch;
}

#unity-canvas {
  width: 100vw;
  height: 100vh;
  display: block;
  background: var(--unity-bg);
  outline: none;
}

#unity-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  background: var(--unity-overlay);
  pointer-events: none;
  z-index: 20;
  color: #ffffff;
}

#unity-progress {
  width: min(520px, 85%);
  height: 10px;
  background: var(--progress-track);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--progress-border);
}

#unity-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--progress-fill);
}

#unity-status {
  font-size: 12px;
  opacity: 0.85;
}

/* ===== Chat Overlay (HTML) ===== */
#chat {
  position: fixed;
  right: 12px;
  top: 100px;              /* NEW */
  bottom: 12px;           /* keep */
  width: min(420px, 92vw);

  height: auto;           /* NEW (because top+bottom define height) */
  /* or just remove height entirely */

  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  backdrop-filter: var(--blur);
  overflow: hidden;
  z-index: 50;
  display: flex;
  flex-direction: column;
}


#chatHeader {
  padding: 10px 12px;
  display: flex;
  gap: 8px;
  align-items: center;
  border-bottom: 1px solid var(--border-soft);
}

#chatHeader .title {
  font-weight: 800;
  font-size: 13px;
  opacity: 0.9;
}

#chatHeader .spacer {
  flex: 1;
}

button {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--btn-bg);
  color: var(--btn-text);
  font-weight: 800;
  backdrop-filter: var(--blur);
  cursor: pointer;
  user-select: none;
}

button:hover {
  background: var(--btn-bg-hover);
}

#chatBody {
  padding: 10px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg {
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid var(--msg-border);
  background: var(--msg-bg);
  font-size: 13px;
  line-height: 1.35;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--msg-user-bg);
}

.msg.assistant {
  align-self: flex-start;
}

.msg.system {
  align-self: flex-start;
  opacity: 0.9;
  background-color: rgb(73, 116, 245);
  color: white;
}

/* Collapsed chat button */
#chatCollapsedBtn {
  display: none;
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 55;
  padding: 10px 12px;
  border-radius: 999px;
}

body.chat-collapsed #chat {
  display: none;
}

body.chat-collapsed #chatCollapsedBtn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

/* ===== Mic button (bottom-center) ===== */
#micWrap {
  position: fixed;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  z-index: 80;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

#micBtn {
  width: 64px;
  height: 64px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--mic-idle-bg);
  backdrop-filter: var(--blur);
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: var(--shadow-strong);
}

#micBtn img {
  width: 26px;
  height: 26px;
  opacity: 0.95;
  pointer-events: none;
}

#micHint {
  font-size: 12px;
  opacity: 0.85;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--msg-bg);
  backdrop-filter: var(--blur);
  max-width: min(320px, 92vw);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* mic states */
#micWrap[data-state="idle"] #micBtn {
  background: var(--mic-idle-bg);
}

#micWrap[data-state="recording"] #micBtn {
  background: var(--mic-recording-bg);
  border-color: var(--border-strong);
  box-shadow:
    0 0 0 6px hsl(var(--h-danger) var(--danger-s) var(--danger-l) / 0.2),
    0 12px 34px hsl(0 0% 0% / 0.45);
}

#micWrap[data-state="transcribing"] #micBtn,
#micWrap[data-state="thinking"] #micBtn {
  background: var(--mic-neutral-bg);
}

#micWrap[data-state="speaking"] #micBtn {
  background: var(--mic-speaking-bg);
  box-shadow:
    0 0 0 6px hsl(var(--h-accent) var(--accent-s) var(--accent-l) / 0.18),
    0 12px 34px hsl(0 0% 0% / 0.45);
}

/* ===== Language dropdown (top-right) ===== */
/* ===== Language dropdown (top-right) ===== */
#langWrap {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 9000;
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  background: var(--panel-strong);
  backdrop-filter: var(--blur);
  box-shadow: var(--shadow-soft);
  transition:
    transform 0.2s ease,
    border-color 0.2s ease,
    background 0.2s ease,
    box-shadow 0.2s ease;
}

#langWrap:hover {
  transform: translateY(-1px);
  background: hsl(var(--h-bg) 18% 18% / 0.92);
  border-color: hsl(var(--h-accent) var(--accent-s) var(--accent-l) / 0.35);
  box-shadow:
    0 10px 30px hsl(0 0% 0% / 0.28),
    0 0 0 1px hsl(var(--h-accent) var(--accent-s) var(--accent-l) / 0.08);
}

#langWrap:focus-within {
  border-color: hsl(var(--h-accent) var(--accent-s) var(--accent-l) / 0.5);
  box-shadow:
    0 10px 30px hsl(0 0% 0% / 0.28),
    0 0 0 3px hsl(var(--h-accent) var(--accent-s) var(--accent-l) / 0.16);
}

#langWrap::before {
  content: "";
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-right: 10px;
  pointer-events: none;
}

#langWrap::after {
  content: "▾";
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 10px;
  pointer-events: none;
}

#langSelect {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-weight: 800;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  min-width: 78px;
}

#langSelect option {
  color: #111;
  background: #fff;
}

/* Light theme tuning */
:root[data-theme="light"] #langWrap:hover {
  background: hsl(var(--h-bg) 25% 98% / 0.98);
}

/* PDF Overlay Styling */
.pdf-hidden {
  display: none !important;
}

#pdf-overlay {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(20, 20, 20, 0.95);
  z-index: 99999; /* Highest priority */
}

/* Toolbar */
.pdf-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 15px;
  background: #2c2c2c;
  color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.pdf-title {
  font-size: 14px;
  font-weight: bold;
  color: #ddd;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 60%;
}

/* Toolbar Container */
.pdf-controls {
    display: flex;
    align-items: center; /* Vertically aligns the buttons relative to each other */
}

/* Unified Button Style */
.pdf-controls button {
    display: flex;       /* Flexbox inside the button to center the icon/text */
    align-items: center;
    justify-content: center;
    
    width: 36px;         /* Fixed width */
    height: 36px;        /* Fixed height (makes them square) */
    padding: 0;          /* Remove padding so centering works perfectly */
    margin-left: 8px;    /* Space between buttons */
    
    background: #444;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 20px;     /* Size for the text icons (+, -, ✕) */
    line-height: 1;      /* Removes invisible text spacing */
}

/* Hover Effect */
.pdf-controls button:active { background: #666; }

/* Close Button Color */
.pdf-close-btn { background: #c0392b !important; }

/* Container: Remove 'justify-content' so it doesn't fight the scrollbar */
#pdf-scroll-container {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column; /* Stacks pages vertically */
  align-items: center;    /* Centers pages horizontally */
  padding: 20px;
  -webkit-overflow-scrolling: touch;
  background: rgba(20, 20, 20, 0.1); /* Standard PDF Viewer background color */
}

canvas#pdf-canvas {
  display: block;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
  background-color: white;
}

/* Style for the link inside the chat bubble */
.pdf-chat-link {
  color: #4facfe;
  text-decoration: underline;
  cursor: pointer;
  font-weight: bold;
  display: inline-block;
  padding: 2px 5px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
}
.pdf-chat-link:hover {
  background: rgba(0, 0, 0, 0.4);
}

/* Canvas: Use margin auto to handle centering safely */
#pdf-canvas {
    display: block;
    margin: auto; /* This centers it horizontally AND vertically safely */
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    max-width: none !important; /* Ensure it can grow infinitely */
}
/* PDF Overlay Styling End */
