:root {
  --cream: #fbf3e6;
  --ink: #4a3f4f;
  --ink-soft: #6d5f73;
  --green: #5fb87a;
  --green-light: #a3e4c1;
  --pink: #f7b6d2;
  --pink-deep: #ef8fb8;
  --sky: #a9ddf2;
  --lavender: #cdbcf0;
  --peach: #ffcda3;
  --shadow: rgba(74, 63, 79, 0.18);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

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

html, body {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  overflow: hidden;
  background: #bfe3f5;
  font-family: -apple-system, BlinkMacSystemFont, ui-rounded, "SF Pro Rounded", "Nunito", sans-serif;
  color: var(--ink);
  overscroll-behavior: none;
}

#scene {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
}

/* ---------- loading veil ---------- */
#loadingVeil {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, #bfe3f5 0%, #ffe9c7 100%);
  z-index: 100;
  transition: opacity 0.6s ease;
}
#loadingVeil.hidden { opacity: 0; pointer-events: none; }
.loadingBadge { text-align: center; color: #6d5f73; }
.loadingBlob {
  width: 64px; height: 64px; margin: 0 auto 14px;
  background: #fbf3e6;
  border-radius: 45% 55% 60% 40% / 50% 45% 55% 50%;
  animation: blobPulse 1.6s ease-in-out infinite;
}
@keyframes blobPulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.12); } }
.loadingBadge p { font-size: 15px; font-weight: 600; margin: 0; }

/* ---------- guide bubble ---------- */
.bubble {
  position: fixed;
  top: calc(var(--safe-t) + 18px);
  left: 50%; transform: translateX(-50%);
  max-width: min(78vw, 360px);
  background: #ffffffee;
  border-radius: 20px;
  padding: 12px 18px;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 8px 24px var(--shadow);
  z-index: 40;
  transition: opacity 0.4s ease, transform 0.4s ease;
}
.bubble.hidden { opacity: 0; transform: translateX(-50%) translateY(-10px); pointer-events: none; }

/* ---------- corner buttons ---------- */
.cornerBtn {
  position: fixed;
  top: calc(var(--safe-t) + 14px);
  width: 46px; height: 46px;
  border-radius: 50%;
  border: none;
  background: #ffffffcc;
  box-shadow: 0 4px 14px var(--shadow);
  font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  /* Above .overlay (60): the parent gate is the doctrine's required escape hatch from the
     end-of-session card, so it must stay reachable even while that full-screen overlay is
     showing (and while parent mode itself is open, harmlessly). */
  z-index: 65;
  color: var(--ink-soft);
}
#skyToggle { right: calc(var(--safe-r) + 14px); }
#parentGateIcon { left: calc(var(--safe-l) + 14px); position: fixed; overflow: hidden; }
.gateDot { font-size: 8px; opacity: 0.35; }
.gateRing {
  position: absolute; inset: 0;
  border-radius: 50%;
  background: conic-gradient(var(--green) var(--p, 0%), transparent 0);
  opacity: 0;
  transition: opacity 0.15s;
}
#parentGateIcon.charging .gateRing { opacity: 0.35; }

/* ---------- tonight's list ---------- */
.pillBtn {
  position: fixed;
  bottom: calc(var(--safe-b) + 96px);
  right: calc(var(--safe-r) + 14px);
  border: none; border-radius: 999px;
  background: #ffffffcc;
  box-shadow: 0 4px 14px var(--shadow);
  padding: 9px 16px;
  font-size: 13px; font-weight: 700;
  color: var(--ink-soft);
  z-index: 25;
}
.pillBtn.hidden { display: none; }
#tonightCount {
  display: inline-block; min-width: 16px; padding: 1px 5px;
  background: var(--pink-deep); color: white; border-radius: 999px; font-size: 11px;
}

/* ---------- generic panel (tonight list) ---------- */
.panel {
  position: fixed; z-index: 45;
  left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(88vw, 420px); max-height: 70vh; overflow-y: auto;
  background: white; border-radius: 24px; padding: 22px;
  box-shadow: 0 20px 60px var(--shadow);
}
.panel.hidden { display: none; }
.panel h2 { margin: 0 0 6px; font-size: 19px; }
.panelHint { color: var(--ink-soft); font-size: 13px; margin: 0 0 14px; line-height: 1.4; }
.panel ul, .journalList, .approvalList, .contextList { list-style: none; margin: 0; padding: 0; }
.panel li { background: #f7f2ea; border-radius: 14px; padding: 10px 14px; margin-bottom: 8px; font-size: 14px; }
.closeBtn {
  margin-top: 12px; border: none; background: var(--cream); border-radius: 14px;
  padding: 10px 16px; font-weight: 700; color: var(--ink); width: 100%;
}

/* ---------- request bar ---------- */
#requestBar {
  position: fixed;
  left: 50%; bottom: calc(var(--safe-b) + 18px); transform: translateX(-50%);
  width: min(92vw, 460px);
  display: flex; align-items: center; gap: 8px;
  background: #ffffffee;
  border-radius: 999px;
  padding: 7px 7px 7px 18px;
  box-shadow: 0 10px 30px var(--shadow);
  z-index: 35;
}
#requestInput {
  flex: 1; border: none; outline: none; background: transparent;
  font-size: 16px; font-weight: 600; color: var(--ink);
  font-family: inherit;
  min-width: 0;
}
#requestInput::placeholder { color: #b7abbb; }
.roundBtn {
  flex: none; width: 42px; height: 42px; border-radius: 50%; border: none;
  background: var(--green); color: white; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
}
#micBtn { background: var(--pink); }
#micBtn.listening { animation: micPulse 1s ease-in-out infinite; background: var(--pink-deep); }
#micBtn.hidden { display: none; }
@keyframes micPulse { 0%,100% { box-shadow: 0 0 0 0 rgba(239,143,184,0.5); } 50% { box-shadow: 0 0 0 10px rgba(239,143,184,0); } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 150px); transform: translateX(-50%);
  background: #ffffffee; border-radius: 14px; padding: 8px 16px;
  font-size: 13px; font-weight: 700; color: var(--ink-soft);
  box-shadow: 0 6px 18px var(--shadow);
  z-index: 34; transition: opacity 0.3s ease, transform 0.3s ease;
}
.toast.hidden { opacity: 0; transform: translateX(-50%) translateY(6px); pointer-events: none; }

/* ---------- build-request sparkle (visible feedback within ~1s, per doctrine) ---------- */
.sparkleBurst {
  position: fixed;
  transform: translate(-50%, -50%) scale(0.4);
  font-size: 30px;
  pointer-events: none;
  z-index: 42;
  opacity: 0;
  animation: sparklePop 0.85s ease-out forwards;
  text-shadow: 0 2px 10px rgba(0,0,0,0.15);
}
@keyframes sparklePop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.3) rotate(-8deg); }
  30% { opacity: 1; transform: translate(-50%, -50%) scale(1.2) rotate(4deg); }
  100% { opacity: 0; transform: translate(-50%, -75%) scale(1.05) rotate(0deg); }
}

/* ---------- overlays (end card / parent mode / patch console) ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: rgba(74, 63, 79, 0.28);
  backdrop-filter: blur(6px);
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 12px);
}
.overlay.hidden { display: none; }

.endCardBody {
  text-align: center; color: white;
}
.endCardBlob {
  width: 90px; height: 90px; margin: 0 auto 20px;
  background: #fbf3e6;
  border-radius: 45% 55% 60% 40% / 50% 45% 55% 50%;
  animation: blobPulse 3s ease-in-out infinite;
}
.endCardBody h1 { font-size: 26px; margin: 0 0 8px; }
.endCardBody p { font-size: 16px; opacity: 0.9; margin: 0; }

.parentPanel, .patchPanel {
  width: min(94vw, 480px); max-height: 86vh;
  background: white; border-radius: 26px;
  display: flex; flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 70px rgba(0,0,0,0.35);
}
.parentHeader {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 10px;
}
.parentHeader h1 { font-size: 19px; margin: 0; }
.devTag {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em;
  background: var(--lavender); color: white; border-radius: 999px; padding: 2px 8px; margin-left: 6px;
}
.parentTabs { display: flex; gap: 4px; padding: 0 16px 12px; overflow-x: auto; }
.tab {
  border: none; background: #f2ece0; color: var(--ink-soft);
  border-radius: 999px; padding: 8px 14px; font-size: 13px; font-weight: 700; white-space: nowrap;
}
.tab.active { background: var(--green); color: white; }
.tabPanel { display: none; padding: 4px 20px 20px; overflow-y: auto; }
.tabPanel.active { display: block; }

.journalList li, .approvalList li, .contextList li {
  background: #f7f2ea; border-radius: 14px; padding: 12px 14px; margin-bottom: 8px; font-size: 14px; line-height: 1.4;
}
.journalList .when, .contextList .when { display: block; font-size: 11px; color: var(--ink-soft); margin-top: 2px; }

.approvalList li { display: flex; flex-direction: column; gap: 8px; }
.approvalRow { display: flex; gap: 8px; }
.approvalRow button {
  flex: 1; border: none; border-radius: 10px; padding: 8px; font-weight: 700; font-size: 13px;
}
.approveBtn { background: var(--green-light); color: #1e5c39; }
.rejectBtn { background: #f6d3d3; color: #7a2b2b; }
.emptyState { color: var(--ink-soft); font-size: 13px; padding: 10px 2px; }

#contextForm { display: flex; gap: 8px; margin-bottom: 12px; }
#contextForm input {
  flex: 1; border: 1px solid #e6ddd0; border-radius: 12px; padding: 10px 12px; font-size: 13px; font-family: inherit;
}
#contextForm button {
  border: none; background: var(--green); color: white; border-radius: 12px; padding: 0 16px; font-weight: 700;
}
.contextList li { display: flex; flex-direction: column; gap: 8px; }
.contextTags { display: flex; gap: 6px; flex-wrap: wrap; }
.contextTags label {
  display: flex; align-items: center; gap: 4px;
  background: white; border: 1px solid #e6ddd0; border-radius: 999px; padding: 4px 9px; font-size: 11px;
}
.contextUsage { font-size: 11px; color: var(--ink-soft); font-style: italic; }
.deleteCardBtn { align-self: flex-start; border: none; background: none; color: #a55; font-size: 12px; font-weight: 700; padding: 0; }

.dataActions { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.bigBtn {
  border: none; border-radius: 14px; padding: 13px; font-size: 14px; font-weight: 700;
  background: var(--green); color: white; text-align: center;
}
.bigBtn.subtle { background: #f2ece0; color: var(--ink-soft); }
.fileBtn { position: relative; overflow: hidden; background: var(--sky); color: #1e4a5c; }
.fileBtn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.feedback { font-size: 12px; color: var(--ink-soft); min-height: 16px; }
.credits { margin-top: 8px; font-size: 12px; color: var(--ink-soft); }
.credits ul { margin: 8px 0 0; padding-left: 18px; }
.credits li { margin-bottom: 4px; }

.patchPanel textarea {
  margin: 0 20px; width: calc(100% - 40px);
  height: 120px; border-radius: 14px; border: 1px solid #e6ddd0; padding: 10px;
  font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; resize: vertical;
}
.patchPanel .bigBtn { margin: 12px 20px 0; width: calc(100% - 40px); }
.patchPanel pre {
  margin: 12px 20px 20px; padding: 10px; background: #f7f2ea; border-radius: 12px;
  font-size: 11.5px; max-height: 160px; overflow: auto; white-space: pre-wrap; word-break: break-word;
}

@media (max-width: 420px) {
  .parentPanel, .patchPanel { border-radius: 20px; }
  .bubble { font-size: 14px; }
}
