/* Layout: phone first (app-like, full screen), then wider screens.
   - phone   (< 600px):   top bar + controls, chat fills the rest, input pinned at the bottom
   - tablet  (600-899px): same, centered with a bit more room
   - desktop (>= 900px):  left panel with my big picture and controls, chat on the right */

[hidden] { display: none !important; } /* hidden must win over display: flex etc. */
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", sans-serif; background: #f4f4f6; color: #222;
       -webkit-tap-highlight-color: transparent; }
input, select, button { font: inherit; padding: 10px 12px; border: 1px solid #ccc; border-radius: 10px; background: #fff; color: #222; }
button { background: #222; color: #fff; border: none; cursor: pointer; }
button:disabled { opacity: 0.5; cursor: default; }
.muted { color: #888; }

/* ---------- Login ---------- */
#login-view { max-width: 420px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; gap: 14px;
              padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom)); }
#login-view h1 { text-align: center; margin: 12px 0 4px; }
#subtitle { text-align: center; margin: -10px 0 4px; font-size: 0.95em; }
#register-note { text-align: center; margin: 6px 0 0; padding-top: 16px; border-top: 1px solid #ddd; font-size: 0.85em; color: #222; }
#disclaimer { text-align: center; margin: 0; font-size: 0.75em; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.9em; color: #555; }
.field input, .field select { font-size: 16px; } /* 16px: iPhones don't zoom in when typing */
fieldset.field { border: 1px solid #ddd; border-radius: 12px; padding: 10px 14px; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px 18px; background: #fff; }
fieldset.field label { color: #222; display: flex; align-items: center; gap: 6px; }
#style-text { flex: 1; min-width: 140px; }
#login { padding: 10px; font-size: 1.05em; border-radius: 12px; }
#quick { padding: 12px; border-radius: 12px; background: #222; color: #fff; border: 1px solid #222; }
#quick:hover:not(:disabled) { background: #444; }
#login-error { color: #c00; text-align: center; }
#passcode-dialog { border: none; border-radius: 14px; padding: 18px 20px; width: min(320px, 90vw); box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2); }
#passcode-dialog input { width: 100%; font-size: 18px; letter-spacing: 0.3em; text-align: center; }
#passcode2 { margin-top: 8px; }
#passcode-hint { font-size: 0.85em; margin: 8px 0 0; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 12px; margin-top: 12px; }

/* ---------- Chat: phone (default) ---------- */
#chat-view { height: 100vh; height: 100dvh; display: flex; flex-direction: column; background: #fff; }

#side { display: grid; grid-template-columns: auto auto 1fr; grid-template-areas: "back avatar who" "controls controls controls" "usage usage usage";
        align-items: center; gap: 8px 10px; padding: max(8px, env(safe-area-inset-top)) 12px 8px;
        background: #fff; border-bottom: 1px solid #eee; z-index: 1; }
.back { grid-area: back; background: none; color: #222; font-size: 1.4em; padding: 4px 6px; }
.wide-only { display: none; }
#avatar { grid-area: avatar; width: 48px; height: 48px; border-radius: 50%; object-fit: cover; background: #ddd; }
#avatar.talking { animation: talk 0.6s ease-in-out infinite; }
@keyframes talk { 50% { transform: scale(1.05) translateY(-1px); } }
.who { grid-area: who; display: flex; align-items: center; gap: 8px; min-width: 0; }
.names { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.controls { grid-area: controls; display: flex; gap: 8px; align-items: center; }
.controls select { flex: 1; min-width: 0; padding: 7px 10px; font-size: 14px; }
.voice { display: flex; align-items: center; gap: 4px; font-size: 14px; white-space: nowrap; }
.voice input { width: 18px; height: 18px; }
label.locked { color: #aaa; cursor: help; }

/* Usage bar: a thin line with a small "12% used" label */
.usage { grid-area: usage; display: flex; align-items: center; gap: 8px; cursor: help; }
.usage-track { flex: 1; height: 4px; border-radius: 2px; background: #ececf0; overflow: hidden; }
.usage-fill { height: 100%; width: 0; background: #9aa0a6; border-radius: 2px; transition: width 0.6s ease; }
.usage.warn .usage-fill { background: #f0a020; }
.usage.full .usage-fill { background: #d93025; }
.usage-label { font-size: 11px; color: #999; white-space: nowrap; }
.usage.full .usage-label { color: #d93025; }

.user-photo { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; flex: none; }
.user-photo.big { width: 96px; height: 96px; align-self: center; }
.user-photo.initial { display: inline-flex; align-items: center; justify-content: center; color: #fff; font-weight: bold; }

#chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#log { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 10px;
       font-size: 15px; line-height: 1.4; background: #fff; }
#form { display: flex; gap: 8px; align-items: center; padding: 8px 10px max(8px, env(safe-area-inset-bottom));
        border-top: 1px solid #eee; background: #fff; }
#text { flex: 1; min-width: 0; font-size: 16px; border-radius: 20px; padding: 10px 14px; background: #f4f4f6; border-color: #e6e6ea; }
#form button { border-radius: 20px; padding: 10px 16px; }
#mic { background: #f4f4f6; color: #222; padding: 10px 12px; }
#mic.listening { background: #c00; color: #fff; }

/* Message rows: [my icon][bubble] on the left, [bubble][user icon] on the right (like Instagram). */
.row { display: flex; align-items: flex-end; gap: 8px; margin: 4px 0; }
.row.you { justify-content: flex-end; }
.row .icon { flex: none; width: 30px; height: 30px; border-radius: 50%; object-fit: cover; background: #ddd; }
.row .icon.hidden { visibility: hidden; } /* only the last message in a group shows the icon */
.icon.initial { display: inline-flex; align-items: center; justify-content: center; color: #fff; font-weight: bold; font-size: 13px; }
.msg { padding: 9px 13px; border-radius: 18px; max-width: 78%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.you { background: #222; color: #fff; }
.msg.me { background: #efeff2; }
.msg a { color: #1a5fd0; text-decoration: underline; text-underline-offset: 2px; }
.msg.you a { color: #9cc3ff; }

.options { display: flex; flex-direction: column; gap: 6px; margin: 6px 0 10px 38px; max-width: 85%; }
.options button { background: #fff; color: #222; border: 1px solid #222; text-align: left; padding: 11px 14px; }
.options button:hover:not(:disabled) { background: #222; color: #fff; }
.options button:disabled { opacity: 0.4; cursor: default; }
.options button.typing:disabled { opacity: 1; }
.options.yes-no { flex-direction: row; }
.options.yes-no button { min-width: 80px; text-align: center; }
.scene { display: flex; align-items: center; justify-content: center; width: min(85%, 420px); aspect-ratio: 4 / 3; margin: 6px 0 6px 38px;
         border-radius: 14px; background: #efeff2; color: #888; font-size: 0.9em; overflow: hidden; }
.scene img { width: 100%; height: 100%; object-fit: cover; }

.score { position: relative; display: inline-block; font-weight: bold; font-size: 1.05em; white-space: nowrap; }
.score.bump-up { animation: bump-up 0.6s ease-out; }
.score.bump-down { animation: bump-down 0.5s ease-in-out; }
@keyframes bump-up { 35% { transform: scale(1.3); } }
@keyframes bump-down { 20%, 60% { transform: translateX(-3px); } 40%, 80% { transform: translateX(3px); } }
.pop { position: absolute; right: 0; top: -6px; white-space: nowrap; font-size: 0.95em; font-weight: bold; animation: pop 1.8s ease-out forwards; }
.pop.up { color: #e0245e; text-shadow: 0 0 8px rgba(224, 36, 94, 0.45); }
.pop.down { color: #4a6fa5; }
@keyframes pop { 15% { transform: translateY(-6px) scale(1.15); } to { transform: translateY(-34px); opacity: 0; } }
.divider { text-align: center; color: #aaa; font-size: 0.8em; margin: 10px 0; }
.divider.limit { color: #b00; font-weight: bold; margin: 14px 0; }
.divider.milestone { color: #e0245e; font-size: 0.95em; font-weight: bold; margin: 14px 0; animation: bump-up 0.8s ease-out; }
button.link { background: none; color: #555; padding: 4px 0; }
button.link:hover { color: #000; text-decoration: underline; }

/* ---------- Tablet: same layout, centered, a bit roomier ---------- */
@media (min-width: 600px) {
  #chat-view { max-width: 720px; margin: 0 auto; border-left: 1px solid #eee; border-right: 1px solid #eee; }
  #avatar { width: 60px; height: 60px; }
  .msg { max-width: 70%; }
}

/* ---------- Desktop: left panel + chat ---------- */
@media (min-width: 900px) {
  body { padding: 24px; }
  #chat-view { flex-direction: row; max-width: 1100px; height: calc(100dvh - 48px); margin: 0 auto;
               border: none; border-radius: 18px; overflow: hidden; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08); }
  #side { display: flex; flex-direction: column; align-items: stretch; gap: 16px; width: 320px; flex: none;
          padding: 20px; border-bottom: none; border-right: 1px solid #eee; background: #fafafb; overflow-y: auto; }
  .back { align-self: flex-start; font-size: 0.95em; color: #555; padding: 4px 0; }
  .back:hover { color: #000; }
  .wide-only { display: inline; }
  #avatar { width: 220px; height: 220px; align-self: center; margin-top: 8px; }
  .who { padding: 10px 12px; background: #fff; border-radius: 12px; }
  .user-photo { width: 42px; height: 42px; }
  .controls { flex-direction: column; align-items: stretch; }
  .controls select { padding: 9px 12px; font-size: 15px; }
  .voice { font-size: 15px; padding: 4px 2px; }
  #log { padding: 20px 24px; }
  #form { padding: 12px 20px; }
  .msg { max-width: 65%; }
  #login-view { background: #fff; border-radius: 18px; min-height: auto; margin-top: 4vh; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08); }
}
