/* ── Hanasu — Voice-first learning companion ──────────────────────────
 * Design goals: accessible (high contrast, large targets, keyboard nav),
 * mobile-first (works with one thumb), aesthetic but quiet.
 * ──────────────────────────────────────────────────────────────────── */

:root {
  --bg: #0a0e17;
  --bg-card: #141a2a;
  --bg-elev: #1a2236;
  --primary: #6366f1;
  --primary-hover: #7c80f5;
  --accent: #22d3ee;
  --accent-warm: #f59e0b;
  --text: #e8eaf0;
  --text-sec: #8b8fa8;
  --text-dim: #555a72;
  --success: #22c55e;
  --danger: #ef4444;
  --border: rgba(255,255,255,0.08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --mic-size: 96px;
}

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

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Yu Gothic UI",
               "Meiryo", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* Accessibility — skip link */
.skip-link {
  position: absolute;
  left: 8px;
  top: -40px;
  background: var(--primary);
  color: #fff;
  padding: 8px 14px;
  border-radius: 6px;
  z-index: 100;
  text-decoration: none;
  font-weight: 600;
  transition: top 0.2s;
}
.skip-link:focus { top: calc(var(--safe-top) + 8px); }

/* ── Header ─────────────────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(10,14,23,0.92);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  letter-spacing: 0.2px;
}
.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--primary));
  box-shadow: 0 0 12px rgba(99,102,241,0.5);
}
.brand-name { font-size: 18px; }
.brand-sub {
  font-size: 12px;
  color: var(--text-sec);
  font-weight: 500;
  margin-left: 2px;
}

.header-actions { display: flex; gap: 4px; }

.icon-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}
.icon-btn:hover, .icon-btn:focus-visible {
  background: var(--bg-elev);
  border-color: var(--border);
  outline: none;
}

/* ── Drawer (Sessions / Settings) ──────────────────────────────── */
.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: calc(var(--safe-top) + 16px) 16px 16px;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
  z-index: 40;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.drawer.right {
  left: auto;
  right: 0;
  border-right: none;
  border-left: 1px solid var(--border);
  transform: translateX(100%);
}
.drawer.open { transform: translateX(0); }
.drawer[aria-hidden="true"] { pointer-events: none; }
.drawer.open[aria-hidden="false"] { pointer-events: auto; }

.drawer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.drawer-head h2 { font-size: 16px; font-weight: 700; }

/* Backdrop */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 35;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.backdrop.show { opacity: 1; pointer-events: auto; }

/* Session list */
.sessions-list { display: flex; flex-direction: column; gap: 6px; }
.session-item {
  padding: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  width: 100%;
}
.session-item:hover, .session-item:focus-visible {
  background: var(--bg-card);
  outline: none;
  border-color: var(--primary);
}
.session-item.active {
  border-color: var(--primary);
  background: rgba(99,102,241,0.12);
}
.session-item .s-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .s-meta { font-size: 11px; color: var(--text-sec); display: flex; justify-content: space-between; }
.session-item .s-delete {
  align-self: flex-end;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
}
.session-item .s-delete:hover { color: var(--danger); }

/* Buttons */
.primary-btn {
  background: linear-gradient(135deg, var(--primary), #4f52e0);
  color: #fff;
  border: none;
  padding: 12px 16px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
}
.primary-btn:hover { box-shadow: 0 4px 14px rgba(99,102,241,0.35); }
.primary-btn:active { transform: scale(0.98); }

.danger-btn {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(239,68,68,0.4);
  padding: 10px 14px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  width: 100%;
}
.danger-btn:hover { background: rgba(239,68,68,0.08); }

/* Settings groups */
.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.setting-group label { font-size: 13px; color: var(--text); font-weight: 500; display: flex; justify-content: space-between; align-items: center; }
.setting-group select, .setting-group input[type="range"] {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 14px;
  width: 100%;
}
.setting-group select:focus, .setting-group input[type="range"]:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.setting-group.setting-danger { background: transparent; border: none; padding: 0; }
.setting-footer .tiny { font-size: 11px; color: var(--text-sec); line-height: 1.45; padding: 0 4px; }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 44px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 24px;
  transition: background 0.2s;
}
.slider::before {
  position: absolute; content: ''; height: 18px; width: 18px; left: 2px; top: 2px;
  background: var(--text-sec); border-radius: 50%; transition: transform 0.2s, background 0.2s;
}
.switch input:checked + .slider { background: rgba(99,102,241,0.3); border-color: var(--primary); }
.switch input:checked + .slider::before { transform: translateX(20px); background: var(--primary); }

/* ── Conversation ──────────────────────────────────────────────── */
.conversation {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px 16px 180px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.welcome-card {
  max-width: 560px;
  margin: 24px auto;
  padding: 24px;
  background: linear-gradient(180deg, var(--bg-card), rgba(20,26,42,0.6));
  border: 1px solid var(--border);
  border-radius: 16px;
  text-align: center;
}
.welcome-card h1 { font-size: 24px; margin-bottom: 12px; }
.welcome-card p { color: var(--text-sec); margin-bottom: 16px; }
.example-prompts {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}
.example-prompts li {
  padding: 10px 14px;
  background: var(--bg-elev);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  border: 1px solid var(--border);
}
.hint { font-size: 13px; color: var(--text-sec); margin-top: 8px; }

/* Message bubbles */
.msg {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  padding: 14px 16px;
  border-radius: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: msgIn 0.25s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg.user {
  background: linear-gradient(135deg, rgba(99,102,241,0.18), rgba(99,102,241,0.08));
  border: 1px solid rgba(99,102,241,0.35);
  align-self: flex-end;
  max-width: min(640px, 92%);
  margin-left: auto;
  margin-right: 0;
}
.msg.ai {
  background: var(--bg-card);
  border: 1px solid var(--border);
  align-self: flex-start;
  max-width: min(640px, 92%);
  margin-left: 0;
  margin-right: auto;
  position: relative;
}
.msg .role {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-sec);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 4px;
}
.msg.user .role { color: var(--primary); }
.msg.ai .role { color: var(--accent); }
.msg .replay-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-sec);
  width: 30px;
  height: 30px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.msg .replay-btn:hover { background: var(--bg-elev); color: var(--text); }

.msg.typing::after {
  content: '…';
  display: inline-block;
  animation: dots 1.2s infinite;
  font-weight: 700;
  color: var(--accent);
}
@keyframes dots {
  0%, 20% { content: '.'; }
  40% { content: '..'; }
  60%, 100% { content: '…'; }
}

/* ── Mic zone ──────────────────────────────────────────────────── */
.mic-zone {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 12px calc(16px + var(--safe-bottom));
  background: linear-gradient(180deg, transparent, rgba(10,14,23,0.88) 40%, rgba(10,14,23,0.98));
  z-index: 15;
}

.status-line {
  font-size: 13px;
  color: var(--text-sec);
  min-height: 20px;
  text-align: center;
  font-weight: 500;
  transition: color 0.2s, font-size 0.2s;
}
.status-line.error { color: var(--danger); font-weight: 600; }
.status-line.listening {
  color: var(--success);
  font-weight: 700;
  font-size: 14px;
  animation: statusPulse 2s ease-in-out infinite;
}
.status-line.thinking {
  color: var(--accent-warm);
  font-weight: 600;
}
@keyframes statusPulse {
  0%,100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.interim-line {
  font-size: 15px;
  color: var(--text);
  min-height: 22px;
  text-align: center;
  padding: 0 20px;
  max-width: 600px;
  font-style: italic;
  opacity: 0.85;
  transition: opacity 0.3s;
}
.interim-line:not(:empty) {
  opacity: 1;
  background: rgba(34,197,94,0.08);
  padding: 6px 16px;
  border-radius: 8px;
  border: 1px solid rgba(34,197,94,0.15);
}

.mic-btn {
  width: var(--mic-size);
  height: var(--mic-size);
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #4f52e0);
  border: 3px solid rgba(255,255,255,0.08);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 28px rgba(99,102,241,0.4);
  transition: transform 0.12s, box-shadow 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.mic-btn:hover { box-shadow: 0 10px 32px rgba(99,102,241,0.5); }
.mic-btn:active { transform: scale(0.94); }
.mic-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }

/* ── Recording state: dramatic green glow ── */
.mic-btn.recording {
  background: linear-gradient(135deg, var(--success), #16a34a);
  animation: micPulse 1.8s ease-in-out infinite;
  box-shadow: 0 0 40px rgba(34,197,94,0.6), 0 0 80px rgba(34,197,94,0.2);
}
@keyframes micPulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 40px rgba(34,197,94,0.6), 0 0 80px rgba(34,197,94,0.2); }
  50% { transform: scale(1.06); box-shadow: 0 0 50px rgba(34,197,94,0.8), 0 0 100px rgba(34,197,94,0.3); }
}
.mic-btn.recording::before {
  content: '';
  position: absolute;
  inset: -14px;
  border-radius: 50%;
  border: 3px solid rgba(34,197,94,0.5);
  animation: pulseRing1 1.8s ease-out infinite;
}
.mic-btn.recording::after {
  content: '';
  position: absolute;
  inset: -28px;
  border-radius: 50%;
  border: 2px solid rgba(34,197,94,0.25);
  animation: pulseRing2 1.8s ease-out infinite 0.4s;
}
@keyframes pulseRing1 {
  0% { transform: scale(1); opacity: 0.8; }
  100% { transform: scale(1.6); opacity: 0; }
}
@keyframes pulseRing2 {
  0% { transform: scale(1); opacity: 0.5; }
  100% { transform: scale(1.8); opacity: 0; }
}

/* Audio level ring (set by JS via CSS variable --vol) */
.mic-btn.recording .vol-ring {
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  border: 4px solid var(--success);
  opacity: var(--vol, 0);
  transform: scale(calc(1 + var(--vol, 0) * 0.3));
  transition: opacity 0.1s, transform 0.1s;
  pointer-events: none;
}

/* Screen-wide glow when recording */
body.is-recording::after {
  content: '';
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at center bottom, rgba(34,197,94,0.08) 0%, transparent 60%);
  pointer-events: none;
  z-index: 10;
  animation: bgGlow 2s ease-in-out infinite;
}
@keyframes bgGlow {
  0%,100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* ── Processing state: amber spinner ── */
.mic-btn.processing {
  background: linear-gradient(135deg, var(--accent-warm), #d97706);
  box-shadow: 0 0 30px rgba(245,158,11,0.5);
  position: relative;
}
.mic-btn.processing svg {
  animation: spin 1.5s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Processing dots around button */
.mic-btn.processing::before {
  content: '';
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: var(--accent-warm);
  border-right-color: var(--accent-warm);
  animation: spin 1s linear infinite;
}

.mic-hint { font-size: 12px; color: var(--text-dim); transition: opacity 0.3s; }
body.is-recording .mic-hint { opacity: 0; }

/* Toast */
.toast {
  position: fixed;
  top: calc(var(--safe-top) + 60px);
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: var(--bg-elev);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
  max-width: 90vw;
  text-align: center;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Responsive ────────────────────────────────────────────────── */
@media (min-width: 960px) {
  :root { --mic-size: 112px; }
  .conversation { padding: 28px 20px 200px; }
  .msg { font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  .mic-btn.recording, .mic-btn.recording::before, .msg.typing::after, .mic-btn.processing { animation: none; }
  * { transition-duration: 0.01ms !important; }
}

/* High-contrast focus indicators */
button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
