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

:root {
  --bg0: #0d0d14;
  --bg1: #13131f;
  --bg2: #1a1a2e;
  --bg3: #22223a;
  --bg4: #2a2a45;
  --accent: #7c6cf0;
  --accent2: #9d8ff5;
  --accent-dim: #4a3db3;
  --green: #00c896;
  --red: #e74c3c;
  --orange: #f39c12;
  --text0: #f0f0f5;
  --text1: #b0b0c8;
  --text2: #6a6a8a;
  --border: #2a2a45;
  --radius: 12px;
  --radius-sm: 8px;
}

html { height: 100%; height: -webkit-fill-available; }
body { height: 100%; height: -webkit-fill-available; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg0);
  color: var(--text0);
  font-size: 14px;
  line-height: 1.5;
}

/* ==================== Screens ==================== */
.screen { display: none; height: 100vh; height: 100dvh; }
.screen.active { display: flex; }

/* ==================== Auth / Invite ==================== */
#screen-login, #screen-invite {
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 50% 0%, #1a0a3e 0%, #0d0d14 70%);
  padding-top: env(safe-area-inset-top, 0px);
}

.auth-box {
  width: 100%;
  max-width: 400px;
  padding: 36px;
  background: var(--bg1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.logo { text-align: center; margin-bottom: 32px; }
.logo-icon { font-size: 48px; margin-bottom: 8px; }
.logo h1 { font-size: 28px; letter-spacing: 5px; color: var(--accent); font-weight: 700; }
.subtitle { color: var(--text2); font-size: 12px; margin-top: 4px; }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--text2); }

.input-row { display: flex; gap: 6px; }
.input-row input { flex: 1; }

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 11px 14px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text0);
  font-size: 14px;
  font-family: 'Courier New', monospace;
  transition: border-color .2s;
}
input:focus { outline: none; border-color: var(--accent); }
input::placeholder { color: var(--text2); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 11px 20px; border: none; border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 600; cursor: pointer; transition: all .2s; text-decoration: none;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent2); }
.btn.primary:disabled { background: var(--text2); cursor: not-allowed; }
.btn.secondary { background: var(--bg3); color: var(--text1); border: 1px solid var(--border); }
.btn.secondary:hover { border-color: var(--accent); }
.btn.danger { background: var(--red); color: #fff; }
.btn.danger:hover { background: #c0392b; }
.btn.full { width: 100%; margin-top: 8px; }
.btn.small { padding: 7px 14px; font-size: 12px; }
.btn-icon { background: none; border: none; color: var(--text1); cursor: pointer; font-size: 18px; padding: 6px; border-radius: 6px; transition: all .2s; }
.btn-icon:hover { background: var(--bg3); color: var(--text0); }
.btn-send { width: 44px; height: 44px; border: none; background: var(--accent); color: #fff; border-radius: 50%; cursor: pointer; font-size: 18px; transition: background .2s; flex-shrink: 0; }
.btn-send:hover { background: var(--accent2); }
.btn-close { background: none; border: none; color: var(--text2); cursor: pointer; font-size: 22px; line-height: 1; }
.btn-close:hover { color: var(--text0); }

/* Alerts */
.error-msg { margin-top: 10px; padding: 10px 14px; background: rgba(231,76,60,.12); border: 1px solid rgba(231,76,60,.3); border-radius: var(--radius-sm); color: var(--red); font-size: 13px; }
.info-text { color: var(--text1); font-size: 13px; margin-bottom: 16px; line-height: 1.6; }
.info-text.success { color: var(--green); }
.warning-box { margin: 14px 0; padding: 12px 14px; background: rgba(243,156,18,.1); border: 1px solid rgba(243,156,18,.3); border-radius: var(--radius-sm); color: var(--orange); font-size: 13px; }
.loading { display: flex; align-items: center; gap: 10px; margin-top: 14px; color: var(--text2); font-size: 13px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Key display */
.key-display { margin: 16px 0; padding: 16px; background: var(--bg0); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.key-label { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--text2); margin-top: 10px; }
.key-label:first-child { margin-top: 0; }
.key-value { font-family: 'Courier New', monospace; user-select: all; padding: 6px 0; }
.key-value.name-val { font-size: 20px; color: var(--green); letter-spacing: 2px; }
.key-value.key-val { font-size: 13px; color: var(--accent); word-break: break-all; line-height: 1.8; }
.check-label { display: flex; align-items: center; gap: 8px; margin-top: 14px; color: var(--text1); font-size: 13px; cursor: pointer; }
.check-label input { accent-color: var(--accent); }

/* ==================== Chat Layout ==================== */
.layout { display: flex; width: 100%; height: 100vh; height: 100dvh; }

/* Sidebar */
.sidebar { width: 280px; min-width: 280px; background: var(--bg1); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.sidebar-head { display: flex; align-items: center; padding: 14px 16px; padding-top: calc(14px + env(safe-area-inset-top, 0px)); border-bottom: 1px solid var(--border); gap: 8px; }
.app-name { flex: 1; font-size: 16px; font-weight: 700; letter-spacing: 3px; color: var(--accent); }
.head-actions { display: flex; gap: 4px; }

.user-search { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.user-search input { width: 100%; padding: 8px 12px; background: var(--bg2); border: 1px solid var(--border); border-radius: 20px; color: var(--text0); font-size: 13px; }

.user-list { flex: 1; overflow-y: auto; }
.user-item { display: flex; align-items: center; padding: 12px 16px; cursor: pointer; border-bottom: 1px solid var(--border); gap: 10px; transition: background .15s; }
.user-item:hover { background: var(--bg2); }
.user-item.active { background: var(--bg3); border-left: 3px solid var(--accent); padding-left: 13px; }
.user-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--accent-dim); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 15px; color: #fff; flex-shrink: 0; }
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 14px; font-weight: 500; }
.user-status { font-size: 11px; }
.user-status.online { color: var(--green); }
.user-status.offline { color: var(--text2); }
.user-unread { background: var(--accent); color: #fff; border-radius: 10px; padding: 1px 7px; font-size: 11px; font-weight: 600; }
.user-list-empty { text-align: center; padding: 32px 16px; color: var(--text2); font-size: 13px; }

.sidebar-foot { padding: 12px 16px; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.my-name { font-family: monospace; color: var(--accent); font-size: 13px; }
.conn-dot { width: 10px; height: 10px; border-radius: 50%; }
.conn-dot.online { background: var(--green); }
.conn-dot.offline { background: var(--red); }

/* Chat main */
.chat-main { flex: 1; display: flex; flex-direction: column; background: var(--bg0); }
.chat-welcome { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text2); }
.welcome-icon { font-size: 64px; margin-bottom: 16px; }
.chat-welcome h3 { color: var(--text1); margin-bottom: 8px; }

.chat-head { padding: 14px 20px; padding-top: calc(14px + env(safe-area-inset-top, 0px)); background: var(--bg1); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.partner-name { font-size: 16px; font-weight: 600; }
.e2e-badge { font-size: 12px; color: var(--green); }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }

.bubble { max-width: 60%; padding: 10px 14px; border-radius: 16px; position: relative; }
.bubble.out { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.bubble.in  { align-self: flex-start; background: var(--bg2); color: var(--text0); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble-sender { font-size: 11px; color: rgba(255,255,255,.7); margin-bottom: 2px; }
.bubble.in .bubble-sender { color: var(--accent2); }
.bubble-text { word-break: break-word; font-size: 14px; }
.bubble-time { font-size: 11px; margin-top: 4px; opacity: .6; display: flex; align-items: center; gap: 4px; }

.msg-input-bar { padding: 12px 16px; background: var(--bg1); border-top: 1px solid var(--border); display: flex; gap: 10px; align-items: center; }
.msg-input-bar input { flex: 1; padding: 10px 16px; background: var(--bg2); border: 1px solid var(--border); border-radius: 22px; color: var(--text0); font-size: 14px; }

/* ==================== Modals ==================== */
.modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 100; display: none; }
.modal-backdrop.active { display: block; }

.modal-box { position: relative; z-index: 201; background: var(--bg1); border: 1px solid var(--border); border-radius: var(--radius); width: 100%; max-width: 480px; max-height: 85vh; overflow-y: auto; }
.modal-box.wide { max-width: 700px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 16px; }
.modal-body { padding: 20px; }

/* Admin panel */
.admin-tabs { display: flex; gap: 4px; padding: 12px 20px 0; border-bottom: 1px solid var(--border); }
.atab { padding: 8px 16px; background: none; border: none; color: var(--text2); cursor: pointer; font-size: 14px; border-bottom: 2px solid transparent; margin-bottom: -1px; transition: all .2s; }
.atab.active { color: var(--accent); border-bottom-color: var(--accent); }
.admin-tab-content { display: none; padding: 16px 20px; }
.admin-tab-content.active { display: block; }

.admin-toolbar { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }

.admin-table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; padding: 8px 10px; color: var(--text2); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; border-bottom: 1px solid var(--border); }
td { padding: 10px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg2); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.admin { background: rgba(124,108,240,.2); color: var(--accent2); }
.badge.online { background: rgba(0,200,150,.15); color: var(--green); }
.badge.offline { background: var(--bg3); color: var(--text2); }

.td-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.action-btn { padding: 4px 10px; border: 1px solid var(--border); background: var(--bg2); color: var(--text1); border-radius: 6px; font-size: 12px; cursor: pointer; transition: all .2s; white-space: nowrap; }
.action-btn:hover { border-color: var(--accent); color: var(--accent); }
.action-btn.danger:hover { border-color: var(--red); color: var(--red); }

.invite-result { margin-bottom: 16px; padding: 14px; background: var(--bg0); border: 1px solid var(--accent-dim); border-radius: var(--radius-sm); }
.invite-result-label { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--text2); margin-bottom: 8px; }
.invite-url { font-family: 'Courier New', monospace; font-size: 12px; color: var(--accent); word-break: break-all; margin-bottom: 8px; }

.loading-placeholder { text-align: center; padding: 32px; color: var(--text2); }

/* Info rows */
.info-row { display: flex; justify-content: space-between; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.info-row:last-child { border-bottom: none; }
.il { color: var(--text2); }
.iv { color: var(--text0); font-weight: 500; }
.fingerprint-section { margin-top: 14px; }
.fp-value { font-family: 'Courier New', monospace; font-size: 14px; color: var(--accent); letter-spacing: 2px; margin-top: 6px; word-break: break-all; }

/* Text link */
.text-link { color: var(--text2); font-size: 13px; text-decoration: none; }
.text-link:hover { color: var(--accent2); }

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg4); border-radius: 3px; }

/* ==================== Responsive ==================== */

/* Tablet */
@media (max-width: 900px) {
  .sidebar { width: 220px; min-width: 220px; }
  .bubble { max-width: 75%; }
  .modal-box.wide { max-width: 95vw; }
}

/* Mobile */
@media (max-width: 640px) {
  /* Auth: allow scrolling when keyboard opens */
  body { overflow: auto; }
  #screen-login, #screen-invite {
    align-items: flex-start;
    overflow-y: auto;
    padding: 40px 0 20px;
    height: auto;
    min-height: 100dvh;
  }
  .auth-box {
    max-width: 100%;
    width: calc(100% - 24px);
    margin: 0 auto;
    padding: 28px 20px;
  }

  /* Chat layout: sidebar OR chat — simple display toggle */
  .sidebar { width: 100%; min-width: unset; border-right: none; flex-shrink: 0; }
  .chat-main { display: none; flex: 1; flex-direction: column; min-width: 0; }
  .layout.in-chat .sidebar { display: none; }
  .layout.in-chat .chat-main { display: flex; }

  /* Back button visible on mobile */
  #btn-back { display: inline-flex !important; }

  /* Wider chat bubbles */
  .bubble { max-width: 82%; }

  /* Modals as bottom sheet */
  .modal { align-items: flex-end; padding: 0; }
  .modal-box,
  .modal-box.wide {
    width: 100%;
    max-width: 100%;
    max-height: 90dvh;
    border-radius: var(--radius) var(--radius) 0 0;
  }

  /* iPhone safe area (home bar) */
  .msg-input-bar {
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  }
  #screen-login, #screen-invite {
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  }

  /* Größere Tap-Flächen */
  .btn-icon { padding: 10px; }
  .user-item { padding: 14px 16px; }
  .atab { padding: 10px 14px; }

  /* Kein Zoom beim Antippen von Inputs (iOS zoomt bei font-size < 16px) */
  input[type="text"], input[type="password"] { font-size: 16px; }
}
