:root {
  --bg: #09090b;
  --surface: #18181b;
  --surface-2: #27272a;
  --border: #27272a;
  --border-2: #3f3f46;
  --accent: #a78bfa;
  --accent-dim: rgba(167,139,250,0.12);
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #71717a;
  --danger: #f87171;
  --success: #34d399;
  --radius: 10px;
  --transition: 150ms ease;
}

[data-theme="light"] {
  --bg: #fafafa;
  --surface: #ffffff;
  --surface-2: #f4f4f5;
  --border: #e4e4e7;
  --border-2: #d4d4d8;
  --accent: #7c3aed;
  --accent-dim: rgba(124,58,237,0.08);
  --text: #18181b;
  --text-2: #71717a;
  --text-3: #a1a1aa;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

#app { height: 100%; }

.view { display: none; height: 100%; }
.view.active { display: flex; }
.hidden { display: none !important; }

/* Auth */

#auth-view {
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-container {
  width: 100%;
  max-width: 360px;
}

.auth-header {
  text-align: center;
  margin-bottom: 48px;
}

.auth-logo {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 28px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  letter-spacing: -1px;
}

.auth-header h1 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-2);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

input, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  transition: border-color var(--transition);
  outline: none;
}

input:focus, textarea:focus {
  border-color: var(--accent);
}

input::placeholder, textarea::placeholder {
  color: var(--text-3);
}

textarea {
  resize: vertical;
  min-height: 60px;
}

#login-phrases {
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.7;
}

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 0;
  color: var(--text-3);
  font-size: 12px;
}

.divider::before, .divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.file-upload-area {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px;
  border: 1px dashed var(--border-2);
  border-radius: 6px;
  cursor: pointer;
  color: var(--text-3);
  font-size: 13px;
  transition: border-color var(--transition), color var(--transition);
  margin-bottom: 16px;
}

.file-upload-area:hover {
  border-color: var(--accent);
  color: var(--text-2);
}

#login-file-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  font-size: 0;
  z-index: 1;
}

.phrases-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}

.phrases-subtitle {
  font-size: 13px;
  color: var(--text-3);
  margin-bottom: 20px;
}

.phrases-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 20px;
}

.phrase-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 12px;
}

.phrase-idx {
  color: var(--text-3);
  min-width: 14px;
  text-align: right;
}

.phrase-word {
  color: var(--accent);
}

.auth-footer {
  text-align: center;
  margin-top: 16px;
  font-size: 13px;
  color: var(--text-3);
}

.auth-footer a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

.auth-footer a:hover {
  text-decoration: underline;
}

/* Buttons */

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: opacity var(--transition);
}

.btn-primary:hover { opacity: 0.9; }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-outline {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 20px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-2);
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
  margin-bottom: 8px;
}

.btn-outline:hover {
  border-color: var(--accent);
  color: var(--text);
}

.btn-outline-sm {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: transparent;
  color: var(--text-3);
  font-size: 11px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-outline-sm:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.btn-outline-sm.logout-danger:hover {
  border-color: var(--danger);
  color: var(--danger);
  background: rgba(248,113,113,0.08);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}

.btn-icon:hover {
  border-color: var(--accent);
  color: var(--text);
}

/* App Layout */

#app-view {
  display: none;
}

#app-view.active {
  display: flex;
}

#sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 10px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px 20px;
  font-weight: 600;
  font-size: 16px;
  letter-spacing: -0.3px;
}

.sidebar-logo {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  letter-spacing: -0.5px;
}

.sidebar-balance {
  padding: 16px 10px;
  margin-bottom: 8px;
}

.balance-label {
  display: block;
  font-size: 11px;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 2px;
}

.balance-amount {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.balance-amount::after {
  content: ' cr';
  font-size: 14px;
  font-weight: 500;
  color: var(--text-3);
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-2);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  text-align: left;
}

.nav-item:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-item.active {
  background: var(--accent-dim);
  color: var(--accent);
}

.nav-item .badge {
  margin-left: auto;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 8px;
  min-width: 16px;
  text-align: center;
  line-height: 14px;
}

.badge-mob {
  position: absolute;
  top: 2px;
  right: 50%;
  transform: translateX(10px);
  background: var(--accent);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  padding: 0 5px;
  border-radius: 7px;
  min-width: 14px;
  text-align: center;
  line-height: 14px;
}

.logout-item {
  margin-top: auto;
  color: var(--text-3);
}

.logout-item:hover {
  color: var(--danger);
  background: rgba(248,113,113,0.08);
}

/* Content */

#content {
  flex: 1;
  overflow-y: auto;
  padding: 32px;
}

.tab-content {
  display: none;
  max-width: 640px;
}

.tab-content.active {
  display: block;
}

/* Profile */

.profile-card {
  display: flex;
  justify-content: center;
}

.avatar-section {
  text-align: center;
}

.avatar-wrap {
  position: relative;
  width: 96px;
  height: 96px;
  margin: 0 auto 20px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--border);
}

.avatar-wrap:hover .avatar-edit {
  opacity: 1;
}

.avatar-ph {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  color: var(--text-3);
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-edit {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  opacity: 0;
  transition: opacity var(--transition);
  cursor: pointer;
}

#profile-name {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 2px;
}

.profile-id {
  font-size: 12px;
  color: var(--text-3);
  font-family: 'SF Mono', monospace;
  word-break: break-all;
  margin-bottom: 24px;
}

.profile-stats {
  display: flex;
  gap: 32px;
  justify-content: center;
}

.stat {
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.stat-label {
  font-size: 11px;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Cards */

.messages-card, .settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.card-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text);
}

/* Messages */

.messages-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.messages-toolbar .card-title {
  margin-bottom: 0;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-new-msg {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 0;
  border: none;
  background: transparent;
  color: var(--text-3);
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: color var(--transition);
}

.btn-new-msg:hover {
  color: var(--accent);
}

.btn-new-msg svg {
  opacity: 0.7;
  transition: opacity var(--transition);
}

.btn-new-msg:hover svg {
  opacity: 1;
}

.compose-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
  padding: 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.compose-card input,
.compose-card textarea {
  width: 100%;
  padding: 10px;
  font-size: 13px;
  font-family: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  resize: none;
}

.compose-card input:focus,
.compose-card textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.compose-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

.btn-cancel {
  padding: 7px 14px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-3);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}

.btn-cancel:hover { color: var(--text); }

.btn-send {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 16px;
  border: none;
  border-radius: 5px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: opacity var(--transition);
}

.btn-send:hover { opacity: 0.85; }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

.search-field {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-3);
}

.search-field input {
  border: none;
  background: transparent;
  padding: 0;
  font-size: 13px;
  width: 140px;
  color: var(--text);
}

.search-field input:focus { border-color: transparent; outline: none; }

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg-item {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: background var(--transition);
}

.msg-item.unread {
  border-left: 3px solid var(--accent);
  background: var(--accent-dim);
}

.msg-item:hover { background: var(--surface-2); }

.msg-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.msg-sender {
  font-weight: 600;
  font-size: 13px;
}

.msg-sender .dir {
  color: var(--text-3);
  font-weight: 400;
  margin: 0 4px;
}

.msg-sender .other {
  color: var(--accent);
}

.msg-time {
  font-size: 11px;
  color: var(--text-3);
}

.msg-body {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-2);
  margin-bottom: 10px;
}

.msg-body:last-child { margin-bottom: 0; }

.msg-actions {
  display: flex;
  gap: 6px;
}

.btn-reply {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: transparent;
  color: var(--text-3);
  font-size: 11px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-reply:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

.reply-form {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.reply-form textarea {
  width: 100%;
  padding: 10px;
  font-size: 13px;
  font-family: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  resize: none;
  line-height: 1.4;
}

.reply-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.reply-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
}

.btn-reply-send {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  border: none;
  border-radius: 5px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: opacity var(--transition);
}

.btn-reply-send:hover { opacity: 0.85; }
.btn-reply-send:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-reply-cancel {
  padding: 6px 12px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-3);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}

.btn-reply-cancel:hover { color: var(--text-2); }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-3);
  font-size: 13px;
}

/* Settings */

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
}

.setting-row + .setting-row {
  border-top: 1px solid var(--border);
}

.setting-label {
  font-size: 14px;
  font-weight: 500;
}

.setting-desc {
  font-size: 12px;
  color: var(--text-3);
  margin-top: 2px;
}

.mono {
  font-family: 'SF Mono', monospace;
  font-size: 11px;
}

/* Toggle */

.toggle {
  cursor: pointer;
}

.toggle input { display: none; }

.toggle-track {
  display: block;
  width: 40px;
  height: 22px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 11px;
  position: relative;
  transition: background var(--transition);
}

.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--text-3);
  border-radius: 50%;
  transition: transform var(--transition), background var(--transition);
}

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

.toggle input:checked + .toggle-track::after {
  transform: translateX(18px);
  background: #fff;
}

/* Mobile Nav */

#mobile-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 50;
}

.mob-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0;
  border: none;
  background: transparent;
  color: var(--text-3);
  font-size: 10px;
  font-family: inherit;
  cursor: pointer;
  transition: color var(--transition);
  position: relative;
}

.mob-nav.active {
  color: var(--accent);
}

/* Toast */

.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 20px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 100;
  font-size: 13px;
  white-space: nowrap;
  animation: slideUp 200ms ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--success); }

/* Scrollbar */

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 2px; }

/* Mobile */

@media (max-width: 768px) {
  #app-view.active { flex-direction: column; }

  #sidebar {
    display: none;
  }

  #content {
    padding: 16px;
    padding-bottom: 72px;
  }

  #mobile-nav {
    display: flex;
  }

  .auth-container { max-width: 100%; }
  .auth-card { padding: 24px 20px; }

  .phrases-grid { grid-template-columns: 1fr; }

  .messages-toolbar {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .toolbar-right {
    width: 100%;
    justify-content: space-between;
  }

  .search-field { width: 100%; }
  .search-field input { width: 100%; }

  .profile-stats { gap: 24px; }
}

@media (min-width: 769px) {
  #mobile-nav { display: none !important; }
}
