/* ── Design Tokens ──────────────────────────────────────────────── */
:root {
  --bg-primary: #FDF6F0;
  --bg-secondary: #F5EDE6;
  --bg-tertiary: #EDE3DB;
  --bg-hover: #EDE3DB;
  --bg-input: #FFFFFF;
  --bg-card: #FFFFFF;

  --text-primary: #4A3F3A;
  --text-secondary: #9A8F87;
  --text-muted: #B0A59E;

  --primary: #C4796B;
  --primary-hover: #B5685A;
  --primary-rgb: 196, 121, 107;
  --accent: #C4796B;
  --accent-hover: #B5685A;
  --accent-subtle: rgba(196, 121, 107, 0.08);

  --bubble-outbound: #FFE4E1;
  --bubble-inbound: #FFFFFF;
  --bubble-border: #E8DDD5;

  --send-btn: #E8A598;
  --send-btn-hover: #D4897A;

  --success: #7eb896;
  --error: #D4897A;

  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
  --font-size-sm: 0.8125rem;
  --font-size-base: 0.9375rem;
  --font-size-lg: 1.125rem;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 22px;

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

/* ── Reset ─────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--font-size-base);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* Prevent pull-to-refresh on iOS */
  overscroll-behavior: none;
}

input, button, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

a {
  color: var(--accent);
  text-decoration: none;
}

.hidden {
  display: none !important;
}

/* ── App Shell ──────────────────────────────────────────────────── */
#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.screen {
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ── Login Screen ──────────────────────────────────────────────── */
.login-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  max-width: 360px;
  margin: 0 auto;
  width: 100%;
}

.login-logo {
  margin-bottom: 1rem;
  animation: fadeInUp 0.6s ease-out;
}

.login-container h1 {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.login-subtitle {
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  margin-bottom: 2rem;
}

#login-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.input-group {
  position: relative;
}

.input-group input {
  width: 100%;
  padding: 0.85rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--font-size-base);
  transition: border-color 0.2s, box-shadow 0.2s;
  outline: none;
}

.input-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}

.input-group input::placeholder {
  color: var(--text-muted);
}

.pw-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.pw-wrapper input {
  padding-right: 2.5rem;
}

.eye-btn {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.1rem;
  padding: 4px;
  line-height: 1;
  opacity: 0.5;
  transition: opacity 0.15s;
  color: var(--text-secondary);
}

.eye-btn:hover {
  opacity: 1;
}

.error-text {
  color: var(--error);
  font-size: var(--font-size-sm);
  text-align: center;
  min-height: 1.2em;
}

#login-button {
  width: 100%;
  padding: 0.85rem;
  background: var(--accent);
  color: #1a1a2e;
  border: none;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: var(--font-size-base);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

#login-button:hover {
  background: var(--accent-hover);
}

#login-button:active {
  transform: scale(0.98);
}

#login-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Chat Screen Header ────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  padding-top: calc(0.75rem + var(--safe-top));
  background: var(--bg-secondary);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  flex-shrink: 0;
}

.message-list {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 1rem;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.header-top-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot.connected {
  background: var(--success);
  box-shadow: 0 0 6px rgba(126, 184, 150, 0.5);
}

.dot.disconnected {
  background: var(--text-muted);
}

.connection-label {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.mood-badge {
  font-size: 0.8rem;
  padding: 2px 8px;
  border-radius: 12px;
  background: rgba(212,165,116,0.12);
  color: var(--accent);
  animation: fadeIn 0.4s ease-out;
  margin-left: 4px;
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-icon.btn-disabled {
  opacity: 0.3;
  pointer-events: none;
}

#messages-container {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 640px;
  margin: 0 auto;
  width: 100%;
}

/* ── Empty State ───────────────────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 3rem 2rem;
  animation: fadeIn 0.5s ease-out;
}

.empty-icon {
  margin-bottom: 1rem;
  opacity: 0.5;
}

.empty-title {
  font-size: var(--font-size-lg);
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

.empty-desc {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  max-width: 280px;
  line-height: 1.5;
}

/* ── Message Bubbles ───────────────────────────────────────────── */
.message {
  display: flex;
  animation: fadeInUp 0.3s ease-out;
}

.message.outbound {
  justify-content: flex-end;
}

.message.inbound {
  justify-content: flex-start;
}

.bubble {
  max-width: 80%;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius-lg);
  position: relative;
  word-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.5;
}

.message.outbound .bubble {
  background: var(--bubble-outbound);
  border: 1px solid var(--bubble-border);
  border-bottom-right-radius: 4px;
}

.message.inbound .bubble {
  background: var(--bubble-inbound);
  border: 1px solid var(--bubble-border);
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

.bubble-content {
  font-size: var(--font-size-base);
}

.bubble-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.35rem;
  margin-top: 0.3rem;
}

.time {
  font-size: 0.6875rem;
  color: var(--text-muted);
}

.msg-actions {
  display: none;
  gap: 2px;
  margin-left: 4px;
}

.bubble:hover .msg-actions,
.message:active .msg-actions {
  display: flex;
}

.act-edit, .act-del {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.65rem;
  padding: 1px 4px;
  border-radius: 3px;
  transition: color 0.15s, background 0.15s;
}

.act-edit:hover { color: var(--accent); background: var(--accent-subtle); }
.act-del:hover { color: var(--error); background: rgba(224,85,106,0.12); }

.stream-cursor {
  animation: blink 0.7s infinite;
  color: var(--accent);
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.status {
  font-size: 0.6875rem;
  color: var(--text-muted);
}

.status.sent {
  color: var(--text-muted);
}

.status.delivered {
  color: var(--accent);
}

/* ── Input Bar ─────────────────────────────────────────────────── */
.input-bar {
  flex-shrink: 0;
  padding: 0.65rem 0.75rem;
  padding-bottom: calc(0.65rem + var(--safe-bottom));
  background: var(--bg-secondary);
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  z-index: 10;
}

.input-wrapper {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: 640px;
  margin: 0 auto;
  width: 100%;
}

#message-input {
  flex: 1;
  padding: 0.7rem 1rem;
  background: var(--bg-input);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-xl);
  color: var(--text-primary);
  font-size: var(--font-size-base);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  min-width: 0;
}

#message-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}

#message-input::placeholder {
  color: var(--text-muted);
}

#message-input:disabled {
  opacity: 0.5;
}

.btn-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--accent);
  color: #1a1a2e;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s, opacity 0.2s;
}

.btn-send:hover {
  background: var(--accent-hover);
}

.btn-send:active {
  transform: scale(0.92);
}

.btn-send:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  transform: none;
}

/* ── Animations ────────────────────────────────────────────────── */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ── Scrollbar ─────────────────────────────────────────────────── */
.message-list::-webkit-scrollbar {
  width: 4px;
}

.message-list::-webkit-scrollbar-track {
  background: transparent;
}

.message-list::-webkit-scrollbar-thumb {
  background: #DDD2CA;
  border-radius: 4px;
}

.message-list::-webkit-scrollbar-thumb:hover {
  background: #C8BBB2;
}

/* ── Responsive ────────────────────────────────────────────────── */
@media (min-width: 640px) {
  .message-list {
    padding: 1.5rem 2rem;
  }

  .input-bar {
    padding: 0.75rem 1.5rem;
    padding-bottom: calc(0.75rem + var(--safe-bottom));
  }

  .bubble {
    max-width: 65%;
  }
}

/* ── Settings Modal ────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--bg-secondary);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  animation: fadeInUp 0.25s ease-out;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.modal-header h2 {
  font-size: var(--font-size-lg);
  font-weight: 600;
  color: var(--text-primary);
}

.modal-header .btn-icon {
  font-size: 1.25rem;
  width: 32px;
  height: 32px;
}

.modal-body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  overflow-y: auto;
}

.modal-hint {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  line-height: 1.5;
}

#settings-textarea {
  width: 100%;
  min-height: 200px;
  padding: 0.85rem;
  background: var(--bg-tertiary);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: var(--font-size-sm);
  font-family: var(--font-sans);
  line-height: 1.6;
  resize: vertical;
  outline: none;
  transition: border-color 0.2s;
}

#settings-textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

.btn-primary {
  padding: 0.65rem 1.25rem;
  background: var(--accent);
  color: #1a1a2e;
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: var(--font-size-sm);
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  padding: 0.65rem 1.25rem;
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-sm);
  font-size: var(--font-size-sm);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.btn-secondary:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.success-text {
  color: var(--success);
  font-size: var(--font-size-sm);
  text-align: center;
}

/* ── Settings Sections ───────────────────────────────────────── */
.section-title {
  font-size: var(--font-size-base);
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.divider {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  margin: 0.25rem 0;
}

.usage-stats {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.usage-row {
  display: flex;
  justify-content: space-between;
  font-size: var(--font-size-sm);
}

.usage-label {
  color: var(--text-secondary);
}

.usage-row span:last-child {
  color: var(--text-primary);
  font-weight: 500;
}

#settings-pw-msg {
  text-align: center;
  font-size: var(--font-size-sm);
}

/* ── Theme Picker ────────────────────────────────────────────── */
.theme-colors {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

.theme-row input[type="color"] {
  width: 32px;
  height: 32px;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 6px;
  cursor: pointer;
  background: transparent;
  padding: 2px;
}

#import-msg {
  text-align: center;
  font-size: var(--font-size-sm);
}

/* ── Toggle Switch ───────────────────────────────────────────── */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0;
}

.toggle-label {
  font-size: var(--font-size-sm);
  color: var(--text-primary);
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--bg-tertiary);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 26px;
  transition: background 0.25s, border-color 0.25s;
}

.slider::before {
  content: '';
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform 0.25s, background 0.25s;
}

.switch input:checked + .slider {
  background: var(--accent);
  border-color: var(--accent);
}

.switch input:checked + .slider::before {
  transform: translateX(18px);
  background: #1a1a2e;
}

/* ── Model Selection Cards ────────────────────────────────── */
.model-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.model-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  background: var(--bg-card);
}

.model-card:hover {
  border-color: var(--primary);
  background: var(--bg-hover);
}

.model-card.active {
  border-color: var(--primary);
  background: rgba(var(--primary-rgb, 99, 102, 241), 0.08);
  box-shadow: 0 0 0 1px var(--primary);
}

.model-card input[type="radio"] {
  accent-color: var(--primary);
  width: 18px;
  height: 18px;
  cursor: pointer;
  flex-shrink: 0;
}

.model-info {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.model-label {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
}

.model-detail {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ── Content Row (sidebar + messages) ────────────────────── */
.content-row {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
}

/* ── Chat Sidebar ─────────────────────────────────────────── */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.3s ease, min-width 0.3s ease;
}

.sidebar.collapsed {
  width: 0;
  min-width: 0;
  border-right: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
}

.btn-icon-sm {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.btn-icon-sm:hover { background: var(--primary); color: #fff; border-color: var(--primary); }

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.sidebar-toggle {
  position: absolute;
  left: 260px;
  top: 0.5rem;
  z-index: 100;
  width: 24px; height: 48px;
  border-radius: 0 8px 8px 0;
  border: 1px solid var(--border-color);
  background: var(--bg-secondary);
  color: var(--text-muted);
  font-size: 0.7rem;
  cursor: pointer;
  transition: left 0.3s ease;
}
.sidebar.collapsed ~ .sidebar-toggle { left: 0; }

.window-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  margin-bottom: 2px;
  position: relative;
}
.window-item:hover { background: var(--bg-hover); }
.window-item.active { background: rgba(99,102,241,0.12); }

.window-item-name {
  flex: 1;
  font-size: 0.82rem;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.window-item.active .window-item-name { font-weight: 600; }

.window-item-badge {
  font-size: 0.6rem;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(99,102,241,0.15);
  color: var(--primary);
  white-space: nowrap;
}

.window-item-actions {
  display: none;
  gap: 2px;
}
.window-item:hover .window-item-actions { display: flex; }

.window-action-btn {
  width: 20px; height: 20px;
  border-radius: 4px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.65rem;
  cursor: pointer;
}
.window-action-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
.window-action-btn.danger:hover { color: var(--error); }

/* ── iOS Standalone Mode Tweaks ────────────────────────────────── */
@media (display-mode: standalone) {
  .app-header {
    padding-top: calc(0.75rem + var(--safe-top));
  }
}
