/* =========================================================================
   My Shelly Web — visual design
   - Mobile-first; everything sized to fit a phone in portrait.
   - Glassmorphism on the talkbar/topbar over a soft gradient background.
   - iMessage-style left/right bubbles with the user's bubbles in the
     primary teal and the other-party bubbles in a frosted neutral.
   ======================================================================= */

:root {
  --bg-1: #fcfaf7;
  --bg-2: #e8f1f1;
  --bg-3: #f3e6e9;

  --primary:        #0E6F73;
  --primary-soft:   #76C2C0;
  --on-primary:     #ffffff;
  --tertiary:       #D04A6A;
  --tertiary-soft:  #f5a6b8;

  --surface:        #ffffff;
  --surface-glass:  rgba(255, 255, 255, 0.72);
  --surface-bubble: #ffffff;
  --surface-2:      #f1eee9;
  --text:           #181d1d;
  --text-muted:     #5a6363;
  --outline:        rgba(0, 0, 0, 0.08);

  --ready:    #22c55e;
  --warn:     #f59e0b;
  --error:    #ef4444;

  --shadow-1: 0 1px 2px rgba(15, 25, 25, 0.06),
              0 8px 24px rgba(15, 25, 25, 0.08);
  --shadow-2: 0 4px 18px rgba(14, 111, 115, 0.20);

  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-1: #0c1313;
    --bg-2: #122222;
    --bg-3: #1d1418;
    --surface:        #161b1b;
    --surface-glass:  rgba(22, 27, 27, 0.72);
    --surface-bubble: #1f2525;
    --surface-2:      #232a2a;
    --text:           #e7ecec;
    --text-muted:     #9aa3a3;
    --outline:        rgba(255, 255, 255, 0.10);
    --shadow-1: 0 1px 2px rgba(0,0,0,0.40),
                0 12px 30px rgba(0,0,0,0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  font-family: -apple-system, "SF Pro Text", "Segoe UI", "Pretendard",
               "Noto Sans KR", "Noto Sans JP", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg-1);
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(ellipse 100% 70% at 20% -5%,
      color-mix(in oklch, var(--primary-soft) 35%, transparent), transparent 70%),
    radial-gradient(ellipse 90% 60% at 100% 0%,
      color-mix(in oklch, var(--tertiary-soft) 45%, transparent), transparent 70%),
    linear-gradient(to bottom, var(--bg-1), var(--bg-2) 60%, var(--bg-3));
  background-attachment: fixed;
}

#app {
  display: flex; flex-direction: column;
  height: 100dvh; width: 100%;
  max-width: 720px;
  margin: 0 auto;
}

/* ========================================================================
   Top bar
   ====================================================================== */
.topbar {
  position: sticky; top: 0; z-index: 5;
  padding: calc(8px + var(--safe-top)) 14px 8px;
  display: flex; align-items: center; gap: 10px;
  background: var(--surface-glass);
  backdrop-filter: blur(18px) saturate(160%);
  -webkit-backdrop-filter: blur(18px) saturate(160%);
  border-bottom: 1px solid var(--outline);
}

.title-block { display: flex; align-items: center; gap: 10px; flex: 1; }
.logo {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg,
    color-mix(in oklch, var(--primary) 14%, transparent),
    color-mix(in oklch, var(--tertiary) 14%, transparent));
  padding: 4px;
}
.title-text h1 {
  margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.01em;
}
.subtitle {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--text-muted); margin-top: 1px;
}
.connection {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 999px; font-size: 11px;
  background: color-mix(in oklch, var(--text) 6%, transparent);
}
.connection::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%;
  background: var(--warn);
  box-shadow: 0 0 0 0 currentColor;
}
.connection[data-state="ready"]::before    { background: var(--ready); }
.connection[data-state="offline"]::before  { background: var(--error); }
.connection[data-state="connecting"]::before {
  background: var(--warn);
  animation: pulse 1.2s infinite ease-out;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in oklch, var(--warn) 60%, transparent); }
  100% { box-shadow: 0 0 0 8px transparent; }
}

.actions { display: flex; gap: 4px; }
.icon-btn {
  background: transparent; border: 0;
  width: 36px; height: 36px; border-radius: 12px;
  display: grid; place-items: center;
  font-size: 18px; color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
}
.icon-btn:hover, .icon-btn:active {
  background: color-mix(in oklch, var(--text) 8%, transparent);
}

/* ========================================================================
   Conversation
   ====================================================================== */
.conversation {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0 12px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.empty {
  height: 100%; display: grid; place-items: center;
  padding: 24px;
}
.empty-card {
  text-align: center; max-width: 360px;
}
.empty-logo {
  width: 96px; height: 96px;
  border-radius: 28px;
  margin-bottom: 16px;
  background: var(--surface);
  padding: 8px;
  box-shadow: var(--shadow-1);
}
.empty-card h2 { margin: 0 0 6px; font-size: 20px; }
.empty-card p { color: var(--text-muted); line-height: 1.5; margin: 8px 0; }
.empty-card .hint { font-size: 13px; }

.messages {
  display: flex; flex-direction: column; gap: 10px;
  padding: 8px 12px;
}

.bubble-row { display: flex; gap: 6px; align-items: flex-end; }
.bubble-row.bottom { justify-content: flex-end; }
.bubble-row.top    { justify-content: flex-start; }

.bubble {
  max-width: min(82%, 540px);
  display: flex; flex-direction: column;
}
.bubble-row.bottom .bubble { align-items: flex-end; }
.bubble-row.top    .bubble { align-items: flex-start; }

.bubble-meta {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--text-muted);
  padding: 0 4px 4px;
}
.bubble-meta .lang { font-size: 13px; }
.bubble-meta .editable-hint { opacity: 0.6; font-size: 11px; }

.bubble-body {
  border-radius: 22px;
  padding: 11px 15px;
  line-height: 1.45;
  box-shadow: var(--shadow-1);
  font-size: 16px;
  word-wrap: break-word;
  cursor: default;
}
.bubble-row.bottom .bubble-body {
  background: linear-gradient(155deg, var(--primary), color-mix(in oklch, var(--primary) 80%, #1aa3a8));
  color: var(--on-primary);
  border-bottom-right-radius: 8px;
}
.bubble-row.top .bubble-body {
  background: var(--surface-bubble);
  color: var(--text);
  border: 1px solid var(--outline);
  border-bottom-left-radius: 8px;
}
.bubble-body .src  { display: block; }
.bubble-body .dst  { display: block; margin-top: 6px; font-weight: 600; }

.bubble-row.bottom .bubble-body.editable { cursor: pointer; }
.bubble-row.bottom .bubble-body.editable:active { transform: scale(0.99); }

.bubble.streaming .bubble-body { animation: streamPulse 1.4s ease-in-out infinite; }
@keyframes streamPulse {
  0%, 100% { box-shadow: var(--shadow-1); }
  50%      { box-shadow: var(--shadow-1),
                         0 0 0 2px color-mix(in oklch, var(--primary) 25%, transparent); }
}

.play-btn {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 0; cursor: pointer;
  background: color-mix(in oklch, var(--primary) 16%, transparent);
  color: var(--primary);
  display: grid; place-items: center;
  font-size: 14px;
  flex: 0 0 auto;
  margin-bottom: 4px;
  transition: transform 0.1s;
}
.bubble-row.top .play-btn {
  background: color-mix(in oklch, var(--tertiary) 16%, transparent);
  color: var(--tertiary);
}
.play-btn:active { transform: scale(0.92); }

.typing-dots { display: inline-flex; gap: 4px; }
.typing-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; opacity: 0.6;
  animation: typingBounce 1.0s infinite ease-in-out;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.30s; }
@keyframes typingBounce {
  0%, 80%, 100% { transform: translateY(0);   opacity: 0.5; }
  40%           { transform: translateY(-4px); opacity: 1.0; }
}

/* ========================================================================
   Talkbar
   ====================================================================== */
.talkbar {
  display: flex; gap: 12px;
  padding: 12px 14px calc(14px + var(--safe-bottom));
  background: var(--surface-glass);
  backdrop-filter: blur(18px) saturate(160%);
  -webkit-backdrop-filter: blur(18px) saturate(160%);
  border-top: 1px solid var(--outline);
}

.talk-btn {
  flex: 1;
  position: relative;
  border: 0; border-radius: 22px;
  padding: 12px 6px 14px;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  box-shadow: var(--shadow-1);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  transition: transform 0.08s ease, box-shadow 0.18s ease;
  touch-action: none;
}

.talk-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.talk-btn .ring {
  width: 64px; height: 64px; border-radius: 50%;
  display: grid; place-items: center;
  color: var(--on-primary);
  position: relative;
  background: linear-gradient(155deg, var(--primary), color-mix(in oklch, var(--primary) 80%, #1aa3a8));
  box-shadow: 0 6px 16px color-mix(in oklch, var(--primary) 40%, transparent);
  transition: transform 0.1s;
}
.talk-listen .ring {
  background: linear-gradient(155deg, var(--tertiary), color-mix(in oklch, var(--tertiary) 80%, #b8003c));
  box-shadow: 0 6px 16px color-mix(in oklch, var(--tertiary) 40%, transparent);
}
.mic-icon { width: 26px; height: 26px; }

.talk-btn .label    { font-size: 13.5px; font-weight: 600; margin-top: 4px; }
.talk-btn .sublabel { font-size: 11.5px; color: var(--text-muted); }

.talk-btn.recording .ring { transform: scale(1.08); }
.talk-btn.recording {
  box-shadow: var(--shadow-2);
}
.talk-btn .halo {
  position: absolute; top: 12px; left: 50%;
  width: 64px; height: 64px;
  transform: translateX(-50%);
  border-radius: 50%;
  pointer-events: none;
  opacity: 0;
}
.talk-btn.recording .halo {
  opacity: 1;
  animation: haloPulse 1.4s ease-out infinite;
  background: var(--primary);
}
.talk-listen.recording .halo {
  background: var(--tertiary);
}
@keyframes haloPulse {
  0%   { transform: translateX(-50%) scale(0.9); opacity: 0.45; }
  100% { transform: translateX(-50%) scale(1.9); opacity: 0;    }
}

/* ========================================================================
   Modals (language picker / edit / mic gate)
   ====================================================================== */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: end center;
  background: rgba(15, 25, 25, 0.45);
  backdrop-filter: blur(6px);
  animation: fadeIn 0.18s ease;
}
/* HTML5 [hidden] would normally collapse display, but our `.modal` rule
   above re-declares `display: grid` with the same specificity (0,1,0).
   Browsers resolve same-specificity ties by source order, so the modal's
   `display: grid` wins and the modal stays visible even with the hidden
   attribute set. Force it back to none. */
.modal[hidden] { display: none !important; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal .sheet {
  width: 100%; max-width: 540px;
  background: var(--surface);
  border-radius: 28px 28px 0 0;
  padding: 12px 18px calc(20px + var(--safe-bottom));
  box-shadow: var(--shadow-1);
  animation: slideUp 0.25s cubic-bezier(0.2, 0.7, 0.2, 1);
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }

.modal .dialog {
  width: calc(100% - 32px); max-width: 420px;
  background: var(--surface);
  border-radius: 24px;
  padding: 22px;
  margin: auto;
  align-self: center;
  box-shadow: var(--shadow-1);
  animation: dialogPop 0.18s ease;
}
@keyframes dialogPop {
  from { transform: scale(0.94); opacity: 0; }
  to   { transform: none; opacity: 1; }
}
.modal.center { place-items: center; }

.sheet-handle {
  width: 38px; height: 4px; border-radius: 2px;
  background: color-mix(in oklch, var(--text) 18%, transparent);
  margin: 4px auto 12px;
}
.sheet h2, .dialog h2 { margin: 0 0 4px; font-size: 18px; }
.sheet-desc, .dialog p { color: var(--text-muted); font-size: 13.5px; margin: 0 0 14px; }

.lang-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 16px; }
.lang-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 6px;
  border-radius: 14px;
  cursor: pointer;
  transition: background 0.12s;
}
.lang-row:hover, .lang-row:active {
  background: color-mix(in oklch, var(--text) 6%, transparent);
}
.lang-row.selected { background: color-mix(in oklch, var(--primary) 12%, transparent); }
.lang-flag {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface-2);
  font-size: 22px;
}
.lang-row.selected .lang-flag {
  background: color-mix(in oklch, var(--primary) 20%, transparent);
}
.lang-name { display: flex; flex-direction: column; line-height: 1.2; }
.lang-name .native { font-weight: 600; font-size: 15.5px; }
.lang-name .korean { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.lang-check {
  margin-left: auto; color: var(--primary); font-weight: 700;
  visibility: hidden;
}
.lang-row.selected .lang-check { visibility: visible; }

.sheet-close, .ghost {
  background: transparent; border: 0;
  font-size: 15px; color: var(--text);
  padding: 12px 16px; border-radius: 14px;
  cursor: pointer;
  width: 100%;
}
.sheet-close:hover, .ghost:hover {
  background: color-mix(in oklch, var(--text) 8%, transparent);
}
.dialog-actions {
  display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px;
}
.dialog-actions button {
  border: 0; padding: 10px 16px; border-radius: 12px;
  font-size: 15px; cursor: pointer;
}
.dialog-actions .ghost { width: auto; }
.primary {
  background: var(--primary); color: var(--on-primary);
  font-weight: 600;
}
.primary:hover { filter: brightness(1.05); }

#editText {
  width: 100%;
  border: 1px solid var(--outline);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 16px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  resize: vertical;
  line-height: 1.4;
}
#editText:focus { outline: 2px solid var(--primary); }

/* ========================================================================
   Toast
   ====================================================================== */
.toast {
  position: fixed; left: 50%; bottom: calc(100px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--text); color: var(--bg-1);
  padding: 10px 16px; border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow-1);
  z-index: 60;
  animation: toastSlide 0.18s ease;
}
@keyframes toastSlide {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

/* Hide scrollbar visually but keep usable */
.conversation::-webkit-scrollbar { width: 0; background: transparent; }
