/* Dark, mobile-first chat UI. Colors live in CSS variables so they're easy to tweak. */
:root {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #262a33;
  --text: #e8e9ed;
  --muted: #8b90a0;
  --me: #3b5bdb;          /* user's bubbles */
  --her: #22262f;         /* her bubbles */
  --accent: #e0607e;      /* overwritten by persona.avatar_color */
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}

.app {
  display: flex; flex-direction: column;
  height: 100dvh; max-width: 760px; margin: 0 auto;
  border-left: 1px solid var(--border); border-right: 1px solid var(--border);
}

/* ---- header ---- */
.header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #8e5cf7);
  font-weight: 700; font-size: 20px; color: #fff;
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px color-mix(in srgb, var(--accent) 50%, transparent);
}
.who { flex: 1; min-width: 0; }
.name { font-weight: 650; font-size: 17px; }
.status { color: var(--muted); font-size: 13px; }
.status.typing { color: var(--accent); }
.badge {
  font-size: 11px; font-weight: 700; letter-spacing: .5px; vertical-align: middle;
  padding: 2px 6px; border-radius: 6px; background: #f59f00; color: #1a1a1a;
}
.icon-btn {
  background: transparent; border: 1px solid var(--border); color: var(--text);
  width: 40px; height: 40px; border-radius: 12px; font-size: 18px; cursor: pointer;
}
.icon-btn:hover { background: var(--her); }

.mock-banner {
  background: #3a2a05; color: #ffd8a8; font-size: 13px;
  padding: 6px 16px; border-bottom: 1px solid #5c4308; text-align: center;
}

/* ---- messages ---- */
.messages {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 8px;
}
.msg { display: flex; gap: 8px; align-items: flex-end; max-width: 85%; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg .mini-avatar {
  width: 28px; height: 28px; font-size: 13px; box-shadow: none;
}
.bubble {
  padding: 9px 13px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word;
  animation: pop .18s ease-out;
}
.msg.assistant .bubble { background: var(--her); border-bottom-left-radius: 6px; }
.msg.user .bubble { background: var(--me); color: #fff; border-bottom-right-radius: 6px; }
@keyframes pop { from { transform: scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }

/* typing indicator: three bouncing dots */
.dots { display: inline-flex; gap: 4px; padding: 4px 2px; }
.dots span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.dots span:nth-child(2) { animation-delay: .15s; }
.dots span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* ---- composer ---- */
.composer {
  display: flex; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px;
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 20px; padding: 10px 14px; font: inherit; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send {
  width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none; align-self: flex-end;
  background: var(--accent); color: #fff; font-size: 18px; cursor: pointer;
}
.send:disabled { opacity: .5; cursor: default; }

/* ---- memory panel ---- */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(360px, 100%);
  background: var(--panel); border-left: 1px solid var(--border);
  padding: 16px; display: flex; flex-direction: column; gap: 12px;
  box-shadow: -10px 0 30px rgba(0,0,0,.4); z-index: 10;
}
.panel[hidden] { display: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.panel h2 { font-size: 17px; margin: 0; }
.facts { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; }
.facts li { background: var(--her); padding: 8px 12px; border-radius: 10px; font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.danger {
  margin-top: auto; background: transparent; color: #ff8787;
  border: 1px solid #5c2b2b; border-radius: 10px; padding: 10px; cursor: pointer; font: inherit;
}

@media (max-width: 600px) {
  .app { border: 0; }
  .msg { max-width: 92%; }
}

/* =================================================================== v2: mobile / PWA / voice */

/* Full height that follows the on-screen keyboard (set from JS via visualViewport). */
.app { height: var(--app-height, 100dvh); }
body:not(.login-page) { overflow: hidden; overscroll-behavior: none; }

/* Safe areas (notches, gesture bar) when installed as an app */
.header {
  padding-top: max(12px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  gap: 8px;
}
.header .who { margin-left: 4px; }
.composer {
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
}
.messages { overscroll-behavior: contain; }

/* Photo avatar (falls back to the gradient + initial letter) */
.avatar { overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar.has-photo { background: var(--her); }

.icon-btn { width: 38px; height: 38px; font-size: 17px; flex: none; }
.icon-btn.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, transparent); }

/* Composer: the text box takes all the width; the mic sits small inside its right edge */
.input-wrap { flex: 1; min-width: 0; position: relative; display: flex; }
.input-wrap textarea { flex: 1; width: 100%; }
.input-wrap.with-mic textarea { padding-right: 70px; }
.mic-lang {
  position: absolute; right: 38px; bottom: 11px;
  height: 20px; min-width: 26px; padding: 0 4px; border-radius: 6px;
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  font: 600 10px/18px system-ui, sans-serif; letter-spacing: .3px; cursor: pointer; opacity: .8;
}
.mic-lang:hover, .mic-lang:focus-visible { color: var(--text); opacity: 1; }
.mic-lang[hidden] { display: none; }
.mic-btn {
  position: absolute; right: 6px; bottom: 6px;
  width: 30px; height: 30px; border-radius: 50%; border: 0; padding: 0;
  background: transparent; font-size: 15px; line-height: 30px; cursor: pointer;
  opacity: .55; filter: grayscale(1);
}
.mic-btn:hover, .mic-btn:focus-visible { opacity: .9; }
.mic-btn.listening {
  opacity: 1; filter: none; background: color-mix(in srgb, var(--accent) 30%, transparent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 25%, transparent); } }

/* Small, optional "read aloud" button next to her messages */
.speak-btn {
  align-self: flex-end; flex: none;
  width: 26px; height: 26px; border-radius: 50%; border: 0; padding: 0;
  background: transparent; font-size: 13px; cursor: pointer;
  opacity: .4; filter: grayscale(1);
}
.speak-btn:hover, .speak-btn:focus-visible { opacity: .85; }
.speak-btn.speaking { opacity: 1; filter: none; }
body.hide-speak .speak-btn { display: none; }

/* Settings panel */
.setting-group { display: flex; flex-direction: column; gap: 10px; padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.setting-group[hidden] { display: none; }
.switch { display: flex; align-items: center; gap: 10px; font-size: 15px; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.field select {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px; font: inherit;
}
.panel {
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

/* Login screen */
.login-page { display: grid; place-items: center; min-height: 100dvh; overflow: auto; padding: 24px; }
.login-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--border);
  border-radius: 20px; padding: 28px 24px; display: flex; flex-direction: column; align-items: center; gap: 14px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
}
.login-card .avatar { width: 96px; height: 96px; font-size: 40px; }
.login-card h1 { font-size: 22px; margin: 4px 0 0; }
.login-card p { margin: 0; color: var(--muted); font-size: 14px; text-align: center; }
.login-card form { width: 100%; display: flex; flex-direction: column; gap: 10px; margin-top: 6px; }
.login-card input[type=password] {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 14px; font: inherit; outline: none;
}
.login-card input[type=password]:focus { border-color: var(--accent); }
.login-card button {
  background: var(--accent); color: #fff; border: 0; border-radius: 12px; padding: 12px;
  font: inherit; font-weight: 650; cursor: pointer;
}
.login-card button:disabled { opacity: .6; }
.login-error { color: #ff8787 !important; min-height: 1.2em; }

@media (max-width: 600px) {
  .header { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .msg { max-width: 96%; }
}

/* =================================================================== v3: background photo + photos in chat */

/* Her photo behind the messages (static/background.jpg, set via --chat-bg from JS).
   A dark gradient keeps the bubbles readable; turn it off in ⚙️ Settings. */
body.has-bg .messages {
  background:
    linear-gradient(180deg, rgba(12, 14, 18, .72) 0%, rgba(12, 14, 18, .45) 30%,
                    rgba(12, 14, 18, .55) 65%, rgba(12, 14, 18, .85) 100%),
    var(--chat-bg) center 22% / cover no-repeat,
    var(--bg);
}
body.has-bg .msg.assistant .bubble {
  background: rgba(30, 34, 43, .78);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
}
body.has-bg .msg.user .bubble {
  background: rgba(59, 91, 219, .9);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
}
body.has-bg .speak-btn { opacity: .6; }

/* Photo bubbles */
.msg.assistant .bubble.photo {
  padding: 4px; line-height: 0; background: rgba(30, 34, 43, .78);
}
.bubble.photo img {
  display: block; width: min(250px, 62vw); aspect-ratio: 3 / 4; object-fit: cover;
  border-radius: 14px; cursor: zoom-in; background: #1b1e25;
}
.bubble.photo.broken { line-height: 1.45; padding: 9px 13px; }

/* "φτιάχνει φωτογραφία…" placeholder while the photo is being generated */
.msg.assistant .bubble.photo-pending {
  width: min(250px, 62vw); aspect-ratio: 3 / 4; padding: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  color: var(--muted); font-size: 14px; border-radius: 18px;
  background: linear-gradient(110deg, #22262f 30%, #2e3440 50%, #22262f 70%) 0 0 / 250% 100%;
  animation: shimmer 1.4s linear infinite;
}
.bubble.photo-pending .cam { font-size: 30px; animation: pulse-cam 1.4s ease-in-out infinite; }
@keyframes shimmer { to { background-position: -250% 0; } }
@keyframes pulse-cam { 50% { transform: scale(1.12); } }

/* Full-screen viewer */
.lightbox {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .92);
  display: grid; place-items: center; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
  cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100vw; max-height: 100dvh; object-fit: contain; }

/* 📷 button left of the text box */
.photo-btn {
  width: 44px; height: 44px; border-radius: 50%; flex: none; align-self: flex-end;
  border: 1px solid var(--border); background: transparent; color: var(--text);
  font-size: 19px; cursor: pointer; padding: 0;
}
.photo-btn:hover, .photo-btn:focus-visible { background: var(--her); }
.photo-btn[hidden] { display: none; }

/* =================================================================== v4: gallery + custom background */

.icon-btn[hidden] { display: none; }
#settings { overflow-y: auto; }

/* ⚙️ background section */
.bg-row { display: flex; align-items: center; gap: 12px; }
.bg-preview {
  width: 54px; height: 80px; border-radius: 10px; flex: none; border: 1px solid var(--border);
  background: var(--chat-bg, none) center 22% / cover no-repeat, var(--bg);
}
.bg-title { font-size: 15px; }
.soft-btn {
  background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font: inherit; font-size: 15px; text-align: left; cursor: pointer;
}
.soft-btn:hover:not(:disabled) { background: var(--her); }
.soft-btn:disabled { opacity: .45; cursor: default; }

/* Gallery: full screen grid of thumbnails */
.gallery {
  position: fixed; inset: 0; z-index: 30; background: var(--bg);
  display: flex; flex-direction: column;
}
.gallery[hidden] { display: none; }
.gallery-head {
  display: flex; align-items: center; gap: 10px; background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}
.gallery-head h2 { font-size: 17px; margin: 0; }
.gallery-count { flex: 1; color: var(--muted); font-size: 13px; }
.gallery-pick { margin: 0; padding: 8px 16px; font-size: 14px; text-align: center;
  background: color-mix(in srgb, var(--accent) 16%, var(--panel)); }
.gallery-pick[hidden] { display: none; }
.gallery-grid {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 3px;
  align-content: start; padding: 3px 3px max(3px, env(safe-area-inset-bottom));
}
.thumb {
  position: relative; aspect-ratio: 3 / 4; padding: 0; border: 0; cursor: pointer; overflow: hidden;
  background: linear-gradient(110deg, #1b1e25 30%, #252a33 50%, #1b1e25 70%) 0 0 / 250% 100%;
  animation: shimmer 1.4s linear infinite;
}
.thumb.loaded { animation: none; background: #1b1e25; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: 0; transition: opacity .2s; }
.thumb.loaded img { opacity: 1; }
.thumb:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.gallery.picking .thumb::after {
  content: "🌅"; position: absolute; right: 6px; bottom: 6px; font-size: 16px;
  background: rgba(0,0,0,.55); border-radius: 8px; padding: 2px 5px;
}
.gallery-empty { margin: auto; padding: 24px; text-align: center; color: var(--muted); line-height: 1.7; }
.gallery-empty[hidden] { display: none; }

/* Viewer */
.viewer {
  position: fixed; inset: 0; z-index: 40; background: #000;
  display: flex; flex-direction: column; touch-action: pan-y pinch-zoom;
}
.viewer[hidden] { display: none; }
.viewer-top {
  display: flex; align-items: center; gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
  background: linear-gradient(rgba(0,0,0,.7), rgba(0,0,0,0));
  position: absolute; top: 0; left: 0; right: 0; z-index: 2;
}
.viewer-top .icon-btn { background: rgba(0,0,0,.35); border-color: rgba(255,255,255,.18); }
.viewer-date { flex: 1; font-size: 14px; }
.viewer-pos { color: #bbb; font-size: 13px; }
.viewer-stage { flex: 1; min-height: 0; position: relative; display: grid; place-items: center; overflow: hidden; }
.viewer-stage img { max-width: 100%; max-height: 100%; object-fit: contain; user-select: none; -webkit-user-drag: none;
  transition: transform .18s ease-out, opacity .18s; }
.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 44px; height: 64px; border: 0; border-radius: 12px; cursor: pointer;
  background: rgba(0,0,0,.35); color: #fff; font-size: 34px; line-height: 1;
}
.viewer-nav.prev { left: 8px; }
.viewer-nav.next { right: 8px; }
.viewer-nav:disabled { opacity: 0; pointer-events: none; }
@media (pointer: coarse) { .viewer-nav { width: 36px; height: 52px; font-size: 28px; background: rgba(0,0,0,.25); } }
.viewer-actions {
  display: flex; justify-content: space-around; gap: 4px; background: rgba(12,14,18,.92);
  border-top: 1px solid #222;
  padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}
.v-act {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; min-width: 0;
  background: transparent; border: 0; border-radius: 10px; color: var(--text); text-decoration: none;
  font: inherit; font-size: 20px; padding: 6px 2px; cursor: pointer;
}
.v-act span { font-size: 12px; color: #cfd2da; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.v-act:hover, .v-act:focus-visible { background: #1d2028; }
.v-act[hidden] { display: none; }
.v-act:disabled { opacity: .5; }
.v-danger span { color: #ff8787; }

/* "Deleted from the gallery" chat bubble */
.msg.assistant .bubble.photo-gone { color: var(--muted); font-style: italic; font-size: 14px; }

/* Small confirmation message */
.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 60; background: rgba(30,34,43,.96); color: var(--text); border: 1px solid var(--border);
  padding: 9px 16px; border-radius: 12px; font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  max-width: calc(100vw - 32px); text-align: center;
}
.toast[hidden] { display: none; }

/* =================================================================== v5: natural voice */
.tts-voice, .device-voice { display: flex; flex-direction: column; gap: 10px; }
.tts-voice[hidden], .device-voice[hidden] { display: none; }
/* 🧠 panel: facts + preferences, each deletable */
#panel { overflow-y: auto; }
#panel .facts { overflow: visible; }
.mem-section { display: flex; flex-direction: column; gap: 8px; }
.mem-section h3 { font-size: 15px; margin: 6px 0 0; }
.facts li { display: flex; align-items: flex-start; gap: 8px; }
.facts li .item-text { flex: 1; min-width: 0; }
.facts li .item-del {
  flex: none; width: 26px; height: 26px; margin: -3px -6px -3px 0; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted); font-size: 14px; cursor: pointer;
}
.facts li .item-del:hover, .facts li .item-del:focus-visible { color: #ff8787; background: rgba(255,135,135,.1); }
#prefs li { border-left: 3px solid var(--accent); }
.facts li.empty { background: transparent; color: var(--muted); padding: 0; font-size: 13px; }
.pref-form { display: flex; gap: 8px; }
.pref-form input {
  flex: 1; min-width: 0; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 12px; font: inherit; font-size: 15px; outline: none;
}
.pref-form input:focus { border-color: var(--accent); }
.pref-form button {
  flex: none; width: 42px; border: 0; border-radius: 10px; background: var(--accent); color: #fff;
  font-size: 20px; cursor: pointer;
}
.pref-form button:disabled { opacity: .5; }
.speak-btn.loading { animation: blink 1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: .35; } }

/* "Ξαναδοκίμασε" under her "something went wrong" note / a message that got no reply */
.retry-row { display: flex; padding-left: 36px; }
.retry-btn {
  border: 1px solid var(--accent); background: rgba(255, 255, 255, .92); color: var(--accent);
  border-radius: 16px; padding: 6px 14px; font: inherit; font-size: 14px; cursor: pointer;
}
.retry-btn:active { transform: scale(.97); }
.msg.failed .bubble { opacity: .85; font-style: italic; }
.msg.unsent .bubble { opacity: .6; }
