:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef1f0;
  --border: #dfe4e2;
  --text: #1c2422;
  --text-dim: #6b7573;
  --accent: #06c65a;
  --accent-dim: #06c65a22;
  --bubble-me: #06c65a;
  --bubble-me-text: #ffffff;
  --bubble-peer: #eef1f0;
  --bubble-peer-text: #1c2422;
  --danger: #e0503a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181a;
    --surface: #1c2224;
    --surface-2: #232a2c;
    --border: #2c3436;
    --text: #eef2f1;
    --text-dim: #93a09d;
    --accent: #1fdb74;
    --accent-dim: #1fdb7422;
    --bubble-me: #1fdb74;
    --bubble-me-text: #08210f;
    --bubble-peer: #2a3234;
    --bubble-peer-text: #eef2f1;
    --danger: #ff7a63;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Yu Gothic', 'Segoe UI', sans-serif;
}

button, input {
  font-family: inherit;
  font-size: 14px;
}

.hidden { display: none !important; }

.screen { height: 100vh; height: 100dvh; }

.loading-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
}
.loading-screen .brand { font-size: 20px; letter-spacing: 0.02em; opacity: 0.6; }

/* ===== 認証画面 ===== */
#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px;
}

.brand {
  margin: 0 0 4px;
  font-size: 26px;
  letter-spacing: 0.02em;
  text-align: center;
}

.tagline {
  margin: 0 0 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}

.auth-error {
  background: #e0503a1a;
  color: var(--danger);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}

.hint {
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.6;
  margin: 12px 0;
}

.field { display: block; margin-bottom: 12px; }
.field span { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }
.field input { width: 100%; }

input[type="text"], input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text);
}
.btn:hover { filter: brightness(0.96); }
.btn-block { width: 100%; margin-top: 8px; }
.btn-primary { background: var(--accent); color: #06210f; }
.btn-outline { background: transparent; border: 1px solid var(--border); }
.btn-link { background: transparent; color: var(--accent); font-weight: 500; text-decoration: underline; }
.btn-small { padding: 6px 10px; font-size: 12px; }
.btn-icon { background: transparent; padding: 6px 8px; font-size: 18px; line-height: 1; }

/* ===== アプリ全体レイアウト ===== */
#app-screen {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: 340px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  padding: 14px 0;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-dim);
  font-weight: 600;
  border-bottom: 2px solid transparent;
}
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

.panel { display: none; overflow-y: auto; padding: 12px; flex: 1; }
.panel.active { display: block; }

.section-title {
  font-size: 12px;
  color: var(--text-dim);
  margin: 18px 4px 6px;
}

.list { list-style: none; margin: 0; padding: 0; }

.list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 10px;
  cursor: pointer;
}
.list-row:hover { background: var(--surface-2); }
.list-row.selected { background: var(--accent-dim); }

.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent);
  color: #06210f;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}

.row-main { flex: 1; min-width: 0; }
.row-name { font-weight: 600; font-size: 14px; }
.row-sub {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.unread-badge {
  background: var(--accent);
  color: #06210f;
  font-size: 11px;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 7px;
}

.row-actions { display: flex; gap: 6px; flex-shrink: 0; }

.plaza-row { margin-bottom: 6px; border: 1px solid var(--border); }
.plaza-avatar { background: linear-gradient(135deg, var(--accent), #2b9cff); color: #06210f; font-size: 18px; }
.group-avatar { background: #9b6bff; color: #fff; font-size: 16px; }

.empty-hint { color: var(--text-dim); font-size: 12px; padding: 6px 8px; }

.friend-add { display: flex; gap: 6px; margin-bottom: 8px; }
.friend-add input { flex: 1; }

.group-form {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px;
  margin: 8px 0;
}
.group-form input[type="text"] { margin-bottom: 8px; }
.group-form .list-row { padding: 4px 6px; cursor: default; }
.group-form .list-row:hover { background: none; }
.group-check-label { display: flex; align-items: center; gap: 8px; width: 100%; cursor: pointer; }

.friend-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  font-size: 13px;
}
.friend-preview span { flex: 1; }

.field-error { color: var(--danger); font-size: 12px; margin: 0 0 8px; }
.field-hint { color: var(--text-dim); font-size: 12px; margin: 6px 0 0; }

.settings-block { margin-bottom: 20px; }
.settings-label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.id-row { display: flex; gap: 6px; align-items: center; }
.id-row input { flex: 1; }
#my-public-id, #login-handle-display {
  background: var(--surface-2);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 15px;
  letter-spacing: 0.03em;
}

.passkey-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 4px;
  font-size: 13px;
}

/* ===== チャットパネル ===== */
.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
}

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

.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  flex-shrink: 0;
}
.chat-header #chat-back-btn { display: none; }
#chat-header-name { flex: 1; }

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

.msg-row { display: flex; }
.msg-row.me { justify-content: flex-end; }

.bubble {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-row.me .bubble { background: var(--bubble-me); color: var(--bubble-me-text); border-bottom-right-radius: 4px; }
.msg-row.peer .bubble { background: var(--bubble-peer); color: var(--bubble-peer-text); border-bottom-left-radius: 4px; }

.bubble img, .bubble video { max-width: 240px; max-height: 320px; border-radius: 10px; display: block; }

.msg-meta { font-size: 10px; color: var(--text-dim); margin-top: 2px; }
.msg-row.me .msg-meta { text-align: right; }

.msg-sender-name { font-size: 11px; color: var(--text-dim); margin: 0 0 2px 4px; }

.msg-footer { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.msg-row.me .msg-footer { justify-content: flex-end; }

.like-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 2px 9px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text-dim);
}
.like-btn.liked { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }

.chat-input-form {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-input-form input[type="text"] { flex: 1; }
.file-btn { display: flex; align-items: center; justify-content: center; cursor: pointer; }

/* ===== レスポンシブ(モバイル幅) ===== */
@media (max-width: 760px) {
  .sidebar { width: 100%; }
  #app-screen.chat-open .sidebar { display: none; }
  #app-screen:not(.chat-open) .chat-panel { display: none; }
  .chat-header #chat-back-btn { display: inline-flex; }
}
