/* A.M Chan phone app. Built for thumbs: big targets, safe areas, the keyboard never covers the message box. */
:root {
  --bg: #fff5fa;
  --side: #ffe9f3;
  --panel: #ffffff;
  --line: #f6c9de;
  --user: #ff74a8;
  --user2: #ff9bc0;
  --text: #4b2a3d;
  --muted: #8f6178;
  --accent: #ff5f9e;
  --deep: #d63a6e;
  --green: #14a37f;
  --red: #e0345f;
  --warn: #fff1de;
  --app-h: 100dvh;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1220;
    --side: #26172c;
    --panel: #2a1a31;
    --line: #4a2c55;
    --text: #fbe9f2;
    --muted: #c7a0b7;
    --deep: #ff8fb9;
    --warn: #3a2a1f;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 "Nunito", "Quicksand", "Trebuchet MS", -apple-system, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation; /* no double-tap zoom delay */
  -webkit-text-size-adjust: 100%;
}
.app { height: var(--app-h); display: flex; flex-direction: column; overflow: hidden; }

/* ----- full-page screens ----- */
.screen {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 12px;
  padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left));
}
.screen h1 { margin: 0; font-size: 1.8rem; color: var(--deep); }
.screen p { margin: 0; max-width: 24em; }
.muted { color: var(--muted); }
.big { width: 120px; height: 120px; }
.wobble { animation: wobble 2.4s ease-in-out infinite; }
@keyframes wobble { 50% { transform: rotate(4deg) translateY(-3px); } }
#setupForm { display: flex; flex-direction: column; gap: 12px; width: min(100%, 22em); margin-top: 6px; }
input[type="text"] {
  font: inherit; font-size: 16px; /* 16px stops iOS from zooming in on focus */
  min-height: 52px; padding: 0 18px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); color: var(--text); text-align: center; outline: none;
}
input[type="text"]:focus { border-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 52px; padding: 0 28px;
  border-radius: 999px; border: 1px solid transparent; font: inherit; font-weight: 800; cursor: pointer; text-decoration: none;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: linear-gradient(135deg, #ff6fa5, #ff9bc0); color: #fff; box-shadow: 0 6px 14px rgba(255, 95, 158, 0.32); }
.btn.ghost { background: var(--panel); color: var(--deep); border-color: #f3b9d3; }
.btn.small { min-height: 40px; padding: 0 16px; font-size: 0.9rem; }

/* ----- chat ----- */
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; gap: 10px; flex: none;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: linear-gradient(180deg, var(--panel), var(--side)); border-bottom: 1px solid var(--line);
}
.top img { width: 40px; height: 40px; flex: none; }
.top img.talking { animation: nod 0.7s ease-in-out infinite; }
@keyframes nod { 50% { transform: translateY(-2px) scale(1.05); } }
.who { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.who b { color: var(--deep); font-size: 1.05rem; }
.who small { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seg { margin-left: auto; display: inline-flex; border: 1px solid #f3b9d3; border-radius: 999px; overflow: hidden; background: var(--panel); flex: none; }
.seg button { font: inherit; font-size: 0.85rem; font-weight: 700; min-height: 40px; padding: 0 12px; border: 0; background: transparent; color: var(--deep); cursor: pointer; }
.seg button.on { background: var(--accent); color: #fff; }
.icon { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid #f3b9d3; background: var(--panel); color: var(--deep); font-size: 1.3rem; line-height: 1; cursor: pointer; }

.banner { flex: none; padding: 8px 16px; font-size: 0.9rem; text-align: center; }
.banner.warn { background: var(--warn); color: var(--text); border-bottom: 1px solid var(--line); }

.log {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  display: flex; flex-direction: column; gap: 10px;
  padding: 14px max(14px, env(safe-area-inset-right)) 14px max(14px, env(safe-area-inset-left));
}
.log > :first-child { margin-top: auto; }
.msg { max-width: 86%; padding: 10px 15px; border-radius: 20px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; user-select: text; -webkit-user-select: text; animation: rise 0.18s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.msg.ai { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 7px; }
.msg.me { align-self: flex-end; background: linear-gradient(135deg, var(--user), var(--user2)); color: #fff; border-bottom-right-radius: 7px; }
.msg.err { align-self: center; background: #ffe3ea; color: #a3153d; border: 1px solid #f6a8bc; font-size: 0.92rem; text-align: center; }
@media (prefers-color-scheme: dark) { .msg.err { background: #45202c; color: #ffc4d2; border-color: #7a3347; } }
.msg.info { align-self: center; background: transparent; color: var(--muted); font-size: 0.88rem; text-align: center; }
.msg pre { margin: 8px 0 0; padding: 10px 12px; background: rgba(75, 42, 61, 0.08); border-radius: 12px; overflow-x: auto; font: 0.85rem/1.4 ui-monospace, Menlo, Consolas, monospace; white-space: pre; }
.msg.me pre { background: rgba(255, 255, 255, 0.2); }
.dots { display: inline-flex; gap: 5px; padding: 4px 2px; }
.dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); opacity: 0.4; animation: dot 1.1s infinite; }
.dots i:nth-child(2) { animation-delay: 0.18s; }
.dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes dot { 40% { opacity: 1; transform: translateY(-3px); } }

.queuebar {
  flex: none; display: flex; align-items: center; gap: 12px; margin: 0 max(12px, env(safe-area-inset-left)) 8px;
  padding: 10px 12px 10px 16px; border-radius: 18px; background: var(--warn); border: 1px solid var(--line);
}
.qtext { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.qtext b { color: var(--deep); }
.qtext span { color: var(--muted); font-size: 0.85rem; }

.composer {
  flex: none; display: flex; align-items: flex-end; gap: 8px;
  padding: 8px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: var(--bg); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; min-width: 0; resize: none; font: inherit; font-size: 16px; /* 16px stops iOS from zooming in on focus */
  line-height: 1.35; max-height: 7.5em; min-height: 48px; padding: 12px 16px; border-radius: 24px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text); outline: none; -webkit-appearance: none; appearance: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send {
  flex: none; width: 48px; height: 48px; border-radius: 50%; border: 0; cursor: pointer; font-size: 1.15rem; color: #fff;
  background: linear-gradient(135deg, #ff6fa5, #ff9bc0); box-shadow: 0 6px 14px rgba(255, 95, 158, 0.32);
}
.send:disabled { opacity: 0.45; box-shadow: none; }
.send:active { transform: scale(0.94); }

/* ----- menu sheet ----- */
.sheet { position: fixed; inset: 0; z-index: 20; background: rgba(40, 10, 30, 0.45); display: flex; align-items: flex-end; justify-content: center; }
.sheet-card {
  width: min(100%, 520px); background: var(--panel); border-radius: 24px 24px 0 0; padding: 18px 18px max(18px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 8px; animation: slide 0.2s ease-out;
}
@keyframes slide { from { transform: translateY(40px); opacity: 0; } }
.sheet-card h2 { margin: 0 0 4px; color: var(--deep); text-align: center; }
.row { display: flex; align-items: center; justify-content: center; min-height: 52px; border-radius: 16px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-weight: 700; text-decoration: none; cursor: pointer; }
.row.strong { background: linear-gradient(135deg, #ff6fa5, #ff9bc0); color: #fff; border-color: transparent; }
.hint { margin: 0; padding: 4px 8px; text-align: center; color: var(--muted); font-size: 0.9rem; }

/* narrow phones: tighter header */
@media (max-width: 380px) {
  .seg button { padding: 0 9px; font-size: 0.8rem; }
  .who small { display: none; }
}
/* landscape phones: less chrome */
@media (max-height: 460px) and (orientation: landscape) {
  .top img { width: 32px; height: 32px; }
  .composer textarea { min-height: 40px; padding: 8px 14px; }
  .send { width: 40px; height: 40px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }

/* accounts, chat list */
.screen.scroll { justify-content: flex-start; overflow-y: auto; padding-top: max(28px, env(safe-area-inset-top)); }
.screen.scroll > * { flex: none; }
.tabs { display: inline-flex; border: 1px solid #f3b9d3; border-radius: 999px; overflow: hidden; background: var(--panel); }
.tabs button { font: inherit; font-weight: 800; min-height: 44px; padding: 0 26px; border: 0; background: transparent; color: var(--deep); cursor: pointer; }
.tabs button.on { background: var(--accent); color: #fff; }
#authForm { display: flex; flex-direction: column; gap: 12px; width: min(100%, 24em); text-align: left; }
input[type="password"] { font: inherit; font-size: 16px; min-height: 52px; padding: 0 18px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); color: var(--text); outline: none; text-align: center; }
input[type="password"]:focus { border-color: var(--accent); }
#authForm input[type="text"] { text-align: center; }
.notice { margin: 0; padding: 12px 14px; border-radius: 16px; background: var(--warn); border: 1px solid var(--line); font-size: 0.88rem; line-height: 1.4; }
.check { display: flex; align-items: flex-start; gap: 10px; font-weight: 700; min-height: 44px; align-items: center; }
.check input { width: 24px; height: 24px; accent-color: var(--accent); flex: none; }
.error { margin: 0; color: var(--red); font-weight: 700; text-align: center; }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.pad { padding: 30px 10px; }
.link { background: none; border: 0; color: var(--muted); font: inherit; text-decoration: underline; min-height: 44px; cursor: pointer; }
.list { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left)); display: flex; flex-direction: column; gap: 10px; }
.chatrow { display: flex; gap: 8px; align-items: stretch; }
.chatopen { flex: 1; min-width: 0; text-align: left; display: flex; flex-direction: column; gap: 2px; padding: 14px 16px; border-radius: 20px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit; cursor: pointer; box-shadow: 0 3px 10px rgba(255, 95, 158, 0.1); }
.chatopen b { color: var(--deep); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatopen small { color: var(--muted); }
.chatdel { flex: none; min-width: 52px; border-radius: 20px; border: 1px solid var(--line); background: var(--panel); font: inherit; font-size: 1.1rem; cursor: pointer; color: var(--muted); }
.chatdel.sure { background: var(--red); color: #fff; border-color: var(--red); font-size: 0.8rem; font-weight: 800; }
.listfoot { flex: none; display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 10px max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); border-top: 1px solid var(--line); background: var(--bg); }
.listfoot .btn { width: 100%; }
.listfoot .btn:disabled { opacity: 0.45; box-shadow: none; }
/* ----- roleplay stories ----- */
.storyform { display: flex; flex-direction: column; gap: 12px; width: min(100%, 26em); text-align: left; }
.storyform label { display: flex; flex-direction: column; gap: 6px; font-weight: 700; color: var(--deep); }
.storyform input[type="text"] { text-align: left; }
.storyform textarea, .storyform select {
  font: inherit; font-size: 16px; padding: 12px 16px; border-radius: 18px; border: 1px solid var(--line); background: var(--panel); color: var(--text); outline: none; font-weight: 400; resize: vertical;
}
.storyform select { min-height: 52px; border-radius: 999px; }
.storyform textarea:focus, .storyform select:focus { border-color: var(--accent); }
.notes-card { max-height: 85vh; }
.notesbody { overflow-y: auto; display: flex; flex-direction: column; gap: 12px; padding: 4px 2px; text-align: left; }
.notesbody h3 { margin: 0 0 4px; font-size: 0.95rem; color: var(--deep); }
.notesbody p { margin: 0 0 6px; font-size: 0.9rem; line-height: 1.4; overflow-wrap: anywhere; }
.chatrow .tag { display: inline-block; margin-right: 6px; padding: 1px 8px; border-radius: 999px; background: var(--accent); color: #fff; font-size: 0.72rem; font-weight: 800; vertical-align: middle; }
