/* Buddy Bear — kid-friendly, big-touch, calm palette.
   Palette: soft blue + cream + warm brown. No neon, no cartoon rainbow. */

:root {
  --bg: #fdf8ee;
  --card: #ffffff;
  --ink: #2b2118;
  --mute: #7a6e63;
  --accent: #6aa7d8;
  --accent-ink: #1e4a6f;
  --user: #eaf4fc;
  --assistant: #fff6e0;
  --danger: #c65a5a;
  --focus: #2b78c2;
  --radius: 18px;
  --touch: 72px;
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang HK", "PingFang TC",
    "Noto Sans CJK TC", "Helvetica Neue", Arial, sans-serif;
  font-size: 20px;
  line-height: 1.45;
  overscroll-behavior: none;
}

body.child {
  overflow: hidden;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  border: 0;
  padding: 0;
  clip: rect(0 0 0 0);
  overflow: hidden;
}

/* ---- App container ---- */

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.card {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  gap: 12px;
}

.bear {
  font-size: 96px;
  line-height: 1;
}

h1 {
  margin: 0;
  font-size: 36px;
  color: var(--accent-ink);
  letter-spacing: 0.5px;
}

.pin-prompt {
  margin: 0;
  color: var(--mute);
  font-size: 22px;
}

/* ---- PIN pad ---- */

.pin-display {
  display: flex;
  gap: 16px;
  margin: 8px 0 4px 0;
}
.pin-display span {
  display: inline-block;
  width: 36px;
  height: 44px;
  border-bottom: 4px solid var(--accent);
  font-size: 32px;
  line-height: 44px;
  text-align: center;
  color: var(--accent-ink);
}

.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, var(--touch));
  grid-auto-rows: var(--touch);
  gap: 14px;
  margin-top: 8px;
}

.pin-key {
  font-size: 30px;
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 2px solid #e6ddcd;
  border-radius: var(--radius);
  box-shadow: 0 2px 0 #e6ddcd;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.1s ease;
}
.pin-key:active {
  transform: translateY(1px);
  background: #f2ecde;
}
.pin-clear {
  color: var(--danger);
}
.pin-enter {
  color: var(--accent-ink);
  background: #dfeefb;
  border-color: #c1dff5;
}

.pin-message {
  min-height: 1.2em;
  margin: 8px 0 0 0;
  color: var(--danger);
  font-size: 18px;
}

/* ---- Chat ---- */

/* #chat-screen must be a flex column child of #app so that .messages inside
   it can claim flex:1 and then actually scroll. Without this, .messages has
   no height constraint and long replies overflow past body.child's
   `overflow: hidden`, cropping the visible text. */
#chat-screen {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.chat-header {
  flex: 0 0 auto;
  padding: 14px 18px 8px 18px;
  border-bottom: 1px solid #efe6d4;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.chat-header h1 {
  font-size: 24px;
}
.header-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}
.session-meta {
  font-size: 15px;
  color: var(--mute);
}
.read-toggle {
  border: 2px solid #e6ddcd;
  background: #fffdf7;
  color: var(--mute);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  min-height: 32px;
}
.read-toggle[aria-pressed="true"] {
  background: #dfeefb;
  border-color: #c1dff5;
  color: var(--accent-ink);
  font-weight: 600;
}

.messages {
  flex: 1 1 auto;
  /* min-height:0 is required in a nested flex column so the scroll container
     can shrink below its content height — without it, flex:1 is ignored and
     the element grows to fit content, breaking overflow-y. */
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 14px 14px 4px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.bubble {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 22px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble.user {
  align-self: flex-end;
  background: var(--user);
  color: var(--accent-ink);
  border-bottom-right-radius: 6px;
}
.bubble.assistant {
  align-self: flex-start;
  background: var(--assistant);
  color: var(--ink);
  border-bottom-left-radius: 6px;
}
.bubble.pending {
  opacity: 0.85;
  font-style: italic;
  color: var(--mute);
  background: #fff9e8;
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  animation: pending-pulse 1.6s ease-in-out infinite;
}
.bubble.pending .pending-dots {
  display: inline-block;
  min-width: 2.5em;
  font-style: normal;
  letter-spacing: 2px;
  color: var(--accent-ink);
}
.bubble.image {
  padding: 6px;
}
.bubble.image img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 12px;
}
/* Matthew's own photo, shown user-side. */
.bubble.user.image {
  align-self: flex-end;
  background: var(--user);
  max-width: 70%;
}
.bubble.user.image img {
  max-height: 320px;
  object-fit: contain;
}

/* ---- Edit + version pager (message tree) ---- */
.turn-user {
  align-self: flex-end;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  max-width: 85%;
}
.turn-user .bubble.user {
  align-self: flex-end;
  max-width: 100%;
}
/* Assistant turn wrapper: bubble + a read-aloud (🔊) control underneath, left. */
.turn-assistant {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  max-width: 85%;
}
.turn-assistant .bubble.assistant {
  align-self: flex-start;
  max-width: 100%;
}
.assistant-controls {
  padding-left: 4px;
}
.read-aloud {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 18px;
  line-height: 1;
  min-width: 44px;
  min-height: 32px;
  border-radius: 10px;
  cursor: pointer;
}
.read-aloud:active {
  transform: translateY(1px);
}
.read-aloud:disabled {
  opacity: 0.5;
}
.read-aloud.playing {
  color: var(--focus);
  animation: pending-pulse 1.2s ease-in-out infinite;
}
.bubble-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-right: 2px;
}
.edit-btn {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 20px;
  line-height: 1;
  min-width: 44px;
  min-height: 36px;
  border-radius: 10px;
  cursor: pointer;
}
.edit-btn:active {
  transform: translateY(1px);
}
.pager {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: var(--assistant);
  border-radius: 999px;
  padding: 2px 4px;
}
.pager-btn {
  border: 0;
  background: transparent;
  color: var(--accent-ink);
  font-size: 20px;
  line-height: 1;
  min-width: 44px;
  min-height: 32px;
  border-radius: 999px;
  cursor: pointer;
}
.pager-btn:disabled {
  opacity: 0.3;
  cursor: default;
}
.pager-label {
  font-size: 15px;
  color: var(--mute);
  min-width: 30px;
  text-align: center;
}
.edit-chip {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 12px;
  padding: 6px 14px;
  background: var(--user);
  color: var(--accent-ink);
  border-radius: 12px;
  font-size: 15px;
}
.edit-chip button {
  border: 0;
  background: transparent;
  color: var(--accent-ink);
  font-size: 18px;
  min-width: 40px;
  min-height: 32px;
  cursor: pointer;
}

/* Staged photos: up to 10 thumbnails in the composer waiting to be sent
   (ChatGPT-style multi-image). */
.photo-preview {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  margin: 0 12px;
  padding: 6px 10px;
  background: var(--user);
  border-radius: 12px;
}
.photo-thumbs {
  flex: 1 1 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-width: 0;
}
.photo-thumb {
  position: relative;
  flex: 0 0 auto;
}
.photo-thumb img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 8px;
  display: block;
}
.photo-thumb .thumb-x {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  min-width: 20px;
  min-height: 20px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 13px;
  line-height: 20px;
  text-align: center;
  cursor: pointer;
}
.photo-preview-label {
  flex: 1 1 auto;
  font-size: 14px;
  color: var(--accent-ink);
}
#photo-remove {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--accent-ink);
  font-size: 18px;
  min-width: 40px;
  min-height: 32px;
  cursor: pointer;
}

/* A sent multi-photo turn: a right-aligned wrapping grid of thumbnails. */
.msg-photos {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  align-self: flex-end;
  max-width: 100%;
}
.msg-thumb {
  width: 96px;
  height: 96px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--user);
  flex: 0 0 auto;
}
.msg-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

@keyframes pending-pulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

.chat-form {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  padding: 10px 12px 10px 12px;
  border-top: 1px solid #efe6d4;
  background: var(--card);
}
#chat-input {
  flex: 1;
  resize: none;
  border: 2px solid #e6ddcd;
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 22px;
  font-family: inherit;
  background: #fffdf7;
  color: var(--ink);
  min-height: 52px;
}
#chat-input:focus {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
  border-color: var(--focus);
}
#chat-photo,
#chat-mic {
  flex: 0 0 auto;
  width: 56px;
  font-size: 26px;
  background: #fffdf7;
  border: 2px solid #e6ddcd;
  border-radius: 14px;
  cursor: pointer;
  line-height: 1;
}
#chat-photo:active,
#chat-mic:active {
  transform: translateY(1px);
  background: #f2ecde;
}
#chat-photo:disabled,
#chat-mic:disabled {
  opacity: 0.5;
}
/* Mic actively recording — red pulse so it's obvious Buddy Bear is listening. */
#chat-mic.recording {
  background: #fbe4e4;
  border-color: var(--danger);
  animation: pending-pulse 1.1s ease-in-out infinite;
}
/* Mic transcribing (waiting on Gemini). */
#chat-mic.busy {
  opacity: 0.6;
}
#chat-send {
  flex: 0 0 auto;
  width: 60px;
  font-size: 28px;
  background: var(--accent);
  color: white;
  border: 0;
  border-radius: 14px;
  cursor: pointer;
}
#chat-send:disabled {
  opacity: 0.5;
}

.footer-note {
  flex: 0 0 auto;
  margin: 0;
  padding: 6px 16px 10px 16px;
  font-size: 13px;
  color: var(--mute);
  text-align: center;
  background: var(--bg);
}

/* ---- Admin dashboard ---- */

body.admin {
  background: #f4f1ea;
  font-size: 15px;
  overflow: auto;
}
body.admin main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px;
}
body.admin h1 {
  font-size: 26px;
  margin-bottom: 6px;
}
body.admin h2 {
  font-size: 18px;
  margin: 24px 0 8px 0;
}
.admin-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin: 12px 0;
}
.admin-stat {
  background: white;
  border: 1px solid #e6ddcd;
  border-radius: 10px;
  padding: 12px 14px;
}
.admin-stat .label {
  color: var(--mute);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.admin-stat .value {
  font-size: 28px;
  font-weight: 600;
  color: var(--accent-ink);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border: 1px solid #e6ddcd;
  border-radius: 8px;
  overflow: hidden;
  margin-top: 8px;
}
.admin-table th,
.admin-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid #f0e7d4;
  vertical-align: top;
}
.admin-table th {
  background: #f8f2e5;
  font-weight: 600;
  color: var(--accent-ink);
}
.admin-table tr:last-child td {
  border-bottom: none;
}
.admin-table td.flag {
  color: var(--danger);
  font-weight: 600;
}

.admin-controls {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.admin-controls button {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid #d2c6ae;
  background: white;
  cursor: pointer;
  font-size: 14px;
}
.admin-controls button.danger {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
}
.admin-controls select,
.admin-controls input {
  padding: 7px 10px;
  border: 1px solid #d2c6ae;
  border-radius: 8px;
  font-size: 14px;
}
.killswitch-on {
  color: var(--danger);
  font-weight: 600;
}
.killswitch-off {
  color: #4a7a4a;
}

/* Keyboard focus everywhere */
button:focus-visible,
[role="button"]:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
