:root {
  --ink: #17324A;
  --ink-2: #1E3E59;
  --ink-3: #274B69;
  --paper: #F4F7FB;
  --paper-2: #E9EFF6;
  --line: #DCE4EE;
  --text: #202124;
  --text-soft: #5F6368;
  --text-inverse: #EAF2FA;
  --text-inverse-soft: #A9C1D6;
  --blue: #4A829E;
  --blue-deep: #3A6E88;
  --blue-bright: #1A73E8;
  --coral: #4A829E;
  --marigold: #E3A730;
  --moss: #6B9B4F;
  --teal: #2F9E8F;
  --periwinkle: #4A829E;
  --plum: #9B5FA0;
  --danger: #D14B4B;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-1: 0 1px 2px rgba(23,50,74,0.08), 0 1px 3px rgba(23,50,74,.06);
  --shadow-2: 0 8px 28px rgba(23,50,74,0.18);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Google Sans', Roboto, sans-serif;
  background: var(--ink);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-family: 'Fraunces', Georgia, serif; margin: 0; }
button, input, textarea, select { font-family: inherit; }
.hidden { display: none !important; }

/* ===================== AUTH ===================== */
.auth-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 15% 20%, rgba(74,130,158,.45), transparent 42%),
    radial-gradient(circle at 85% 80%, rgba(74,130,158,.30), transparent 45%),
    var(--ink);
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: #fff;
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  box-shadow: var(--shadow-2);
}
.auth-brand { display: flex; align-items: center; gap: 10px; }
.auth-brand-mark { font-size: 28px; width: 34px; height: 34px; display: inline-flex; }
.auth-brand-mark img { width: 100%; height: 100%; object-fit: contain; }
.auth-brand-name { font-family: 'Fraunces', serif; font-size: 26px; font-weight: 600; color: var(--ink); }
.auth-tagline { color: var(--text-soft); margin: 4px 0 24px; font-size: 14.5px; }
.auth-tabs { display: flex; gap: 4px; background: var(--paper-2); padding: 4px; border-radius: var(--radius-md); margin-bottom: 20px; }
.auth-tab {
  flex: 1; border: none; background: transparent; padding: 8px 0;
  border-radius: 7px; font-weight: 600; font-size: 14px; color: var(--text-soft); cursor: pointer;
}
.auth-tab.active { background: #fff; color: var(--ink); box-shadow: var(--shadow-1); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-soft); }
.auth-form input {
  border: 1.5px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px;
  font-size: 15px; color: var(--text); background: #fff;
}
.auth-form input:focus { outline: 2px solid var(--blue); outline-offset: 1px; border-color: transparent; }
.auth-error { color: var(--danger); font-size: 13px; margin: -4px 0 0; min-height: 1em; }

/* ===================== BUTTONS ===================== */
.btn {
  border: none; border-radius: 20px; padding: 9px 18px;
  font-weight: 600; font-size: 14px; cursor: pointer; transition: transform .05s ease, opacity .15s ease, box-shadow .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--blue); color: #fff; box-shadow: var(--shadow-1); }
.btn-primary:hover { background: var(--blue-deep); }
.btn-ghost { background: transparent; color: var(--blue-deep); border: 1.5px solid var(--line); }
.btn-ghost:hover { background: var(--paper-2); }
.btn-block { width: 100%; padding: 11px 0; margin-top: 4px; }
.btn-small { padding: 6px 12px; font-size: 12.5px; }
.icon-btn { background: none; border: none; font-size: 16px; cursor: pointer; color: var(--text-soft); }

/* ===================== APP SHELL ===================== */
.app { display: flex; height: 100dvh; overflow: hidden; }

/* --- rail --- */
.rail {
  width: 72px; flex-shrink: 0; background: var(--ink);
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 12px 0; overflow-y: auto;
}
.rail-item {
  width: 48px; height: 48px; border-radius: 24px; border: none;
  background: var(--ink-2); color: var(--text-inverse); font-size: 18px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  transition: border-radius .18s ease, background .18s ease;
}
.rail-item:hover, .rail-item.active { border-radius: 16px; background: var(--blue); }
.rail-home { font-size: 20px; }
.rail-divider { width: 32px; height: 2px; background: var(--ink-3); border-radius: 1px; margin: 2px 0; }
.rail-classes { display: flex; flex-direction: column; gap: 10px; }
.rail-add { font-size: 22px; color: var(--moss); }
.rail-add:hover { background: var(--moss); color: #fff; }
.class-avatar {
  width: 48px; height: 48px; border-radius: 24px; border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  font-family: 'Fraunces', serif; font-weight: 600; font-size: 18px; color: #fff;
  transition: border-radius .18s ease;
  position: relative;
}
.class-avatar:hover, .class-avatar.active { border-radius: 16px; }
.class-avatar.active::before {
  content: ''; position: absolute; left: -12px; top: 50%; transform: translateY(-50%);
  width: 4px; height: 24px; background: #fff; border-radius: 0 3px 3px 0;
}
.class-avatar-img { width: 100%; height: 100%; object-fit: cover; }

/* --- home view --- */
.home-view { flex: 1; background: var(--paper); overflow-y: auto; padding: 40px 48px; }
.home-header { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 16px; margin-bottom: 28px; }
.home-header h1 { font-size: 30px; font-weight: 600; color: var(--ink); }
.home-actions { display: flex; gap: 10px; }
.class-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 18px; }
.class-card {
  background: #fff; border-radius: var(--radius-lg); overflow: hidden; cursor: pointer;
  box-shadow: var(--shadow-1); border: 1px solid var(--line); transition: transform .12s ease, box-shadow .12s ease;
}
.class-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-2); }
.class-card-banner { height: 92px; padding: 14px 16px; display: flex; flex-direction: column; justify-content: flex-end; color: #fff; position: relative; }
.class-card-banner h3 { font-size: 18px; font-weight: 600; line-height: 1.2; padding-right: 44px; }
.class-card-avatar {
  position: absolute; right: 14px; bottom: -18px; width: 44px; height: 44px; border-radius: 50%;
  background: #fff; color: var(--ink); display: flex; align-items: center; justify-content: center;
  font-family: 'Fraunces', serif; font-weight: 700; font-size: 17px; box-shadow: var(--shadow-1); overflow: hidden;
}
.class-card-avatar img { width: 100%; height: 100%; object-fit: cover; }
.class-card-body { padding: 20px 16px 14px; }
.class-card-desc { color: var(--text-soft); font-size: 13.5px; margin: 0 0 8px; min-height: 1.3em; }
.class-card-role { font-size: 11.5px; font-weight: 700; letter-spacing: .04em; color: var(--blue-deep); }
.empty-state { text-align: center; margin-top: 80px; color: var(--text-soft); }
.empty-state p:first-child { font-size: 17px; color: var(--text); font-weight: 600; }
.empty-sub { font-size: 14px; }

/* --- class view layout --- */
.class-view { flex: 1; display: flex; overflow: hidden; }
.channel-sidebar { width: 240px; flex-shrink: 0; background: var(--ink-2); display: flex; flex-direction: column; }
.class-banner { padding: 18px 16px 34px; border-bottom: 1px solid rgba(255,255,255,.08); position: relative; }
.class-banner-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.class-banner h2 { color: var(--text-inverse); font-size: 18px; font-weight: 600; line-height: 1.25; }
.class-banner p { color: var(--text-inverse-soft); font-size: 12.5px; margin: 4px 0 0; padding-right: 8px; }
.class-settings-btn { color: var(--text-inverse-soft); font-size: 15px; flex-shrink: 0; }
.class-settings-btn:hover { color: #fff; }
.class-banner-avatar {
  position: absolute; left: 16px; bottom: -18px; width: 40px; height: 40px; border-radius: 50%;
  background: #fff; color: var(--ink); display: flex; align-items: center; justify-content: center;
  font-family: 'Fraunces', serif; font-weight: 700; font-size: 16px; box-shadow: var(--shadow-1); overflow: hidden;
}
.class-banner-avatar img { width: 100%; height: 100%; object-fit: cover; }
.channel-list { flex: 1; overflow-y: auto; padding: 26px 8px 10px; }
.channel-item {
  display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 8px;
  color: var(--text-inverse-soft); font-size: 14.5px; cursor: pointer; margin-bottom: 2px;
}
.channel-item .hash { opacity: .7; font-weight: 600; }
.channel-item:hover { background: var(--ink-3); color: var(--text-inverse); }
.channel-item.active { background: var(--blue); color: #fff; }
.channel-item.stream-type .hash { content: ''; }
.add-channel-btn {
  margin: 0 12px 10px; padding: 6px 8px; border-radius: 8px; text-align: left;
  background: none; border: none; color: var(--text-inverse-soft); font-size: 13px; cursor: pointer;
}
.add-channel-btn:hover { color: #fff; background: var(--ink-3); }
.sidebar-footer { padding: 10px; display: flex; gap: 6px; border-top: 1px solid rgba(255,255,255,.08); }
.sidebar-footer .btn { flex: 1; }

.chat-main { flex: 1; display: flex; flex-direction: column; background: var(--paper); min-width: 0; }
.chat-header {
  height: 56px; flex-shrink: 0; display: flex; align-items: center; gap: 8px;
  padding: 0 18px; border-bottom: 1px solid var(--line); font-weight: 600; background: #fff;
}
.chat-header-hash { color: var(--blue-deep); font-weight: 700; }
.pinned-btn { margin-left: auto; background: none; border: none; font-size: 13px; color: var(--text-soft); cursor: pointer; padding: 6px 10px; border-radius: 14px; }
.pinned-btn:hover { background: var(--paper-2); }

.message-list { flex: 1; overflow-y: auto; padding: 16px 20px 4px; display: flex; flex-direction: column; gap: 2px; }
.msg-group { display: flex; gap: 12px; padding: 6px 6px; border-radius: 8px; }
.msg-group:hover { background: rgba(23,50,74,.035); }
.msg-avatar {
  width: 38px; height: 38px; border-radius: 50%; flex-shrink: 0; margin-top: 2px;
  display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; font-size: 14px;
}
.msg-body { flex: 1; min-width: 0; }
.msg-meta { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.msg-author { font-weight: 700; font-size: 14.5px; color: var(--ink); }
.msg-time { font-size: 11.5px; color: var(--text-soft); }
.msg-text { font-size: 14.5px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.edited-tag { font-size: 11.5px; color: var(--text-soft); font-style: italic; font-weight: 400; }
.msg-pin-flag { font-size: 11px; color: var(--marigold); font-weight: 700; margin-bottom: 2px; }

.msg-embed { margin-top: 8px; max-width: 400px; }
.msg-embed iframe { width: 100%; aspect-ratio: 16/9; border-radius: 10px; border: 1px solid var(--line); }

.msg-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.attachment-img { max-width: 260px; max-height: 200px; border-radius: 10px; border: 1px solid var(--line); display: block; }
.attachment-video { max-width: 320px; max-height: 220px; border-radius: 10px; border: 1px solid var(--line); display: block; background: #000; }
.attachment-audio { display: block; max-width: 300px; }
.attachment-file {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: #fff;
  border: 1px solid var(--line); border-radius: 10px; font-size: 13px; text-decoration: none; color: var(--text);
}

.msg-reactions { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.reaction-pill {
  display: flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 12px;
  background: var(--paper-2); border: 1px solid var(--line); font-size: 12.5px; cursor: pointer;
}
.reaction-pill.mine { background: #E4EEF4; border-color: var(--blue); }
.reaction-pill:hover { border-color: var(--blue); }

.msg-actions {
  display: none; gap: 2px; position: relative; top: -26px; float: right; margin-left: 8px;
  background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-1); padding: 2px;
}
.msg-group:hover .msg-actions { display: flex; }
.msg-actions button { background: none; border: none; padding: 5px 7px; cursor: pointer; border-radius: 6px; font-size: 14px; }
.msg-actions button:hover { background: var(--paper-2); }
.msg-thread-link { margin-top: 4px; font-size: 12.5px; color: var(--blue-deep); font-weight: 600; cursor: pointer; }

.edit-box { display: flex; flex-direction: column; gap: 6px; margin-top: 2px; }
.edit-textarea {
  width: 100%; resize: vertical; min-height: 44px; border: 1.5px solid var(--blue); border-radius: 8px;
  padding: 8px 10px; font-size: 14.5px; font-family: inherit;
}
.edit-actions { display: flex; gap: 6px; justify-content: flex-end; }

.thread-reply-actions { display: flex; gap: 4px; margin-top: 4px; }
.thread-reply-actions button { background: none; border: none; cursor: pointer; padding: 3px 5px; border-radius: 6px; font-size: 12.5px; color: var(--text-soft); }
.thread-reply-actions button:hover { background: var(--paper-2); }
.thread-reply { padding: 4px; border-radius: 8px; }
.thread-reply:hover { background: rgba(23,50,74,.03); }

.typing-indicator { padding: 0 22px; font-size: 12.5px; color: var(--text-soft); font-style: italic; min-height: 18px; }

.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 16px; position: relative; }
.composer-attach { background: none; border: none; font-size: 20px; cursor: pointer; padding: 8px; flex-shrink: 0; color: var(--text-soft); border-radius: 8px; }
.composer-attach:hover { background: var(--paper-2); }
#btn-gif { font-size: 12px; font-weight: 800; letter-spacing: .02em; }
.composer textarea {
  flex: 1; resize: none; border: 1.5px solid var(--line); border-radius: 20px; padding: 10px 16px;
  font-size: 14.5px; max-height: 140px; background: #fff;
}
.composer textarea:focus { outline: 2px solid var(--blue); outline-offset: -1px; }
.composer-send { flex-shrink: 0; }
.attach-preview { display: flex; gap: 8px; padding: 0 16px 10px; flex-wrap: wrap; }
.attach-chip { display: flex; align-items: center; gap: 6px; background: var(--paper-2); border-radius: 8px; padding: 5px 8px; font-size: 12.5px; }
.attach-chip button { background: none; border: none; cursor: pointer; color: var(--danger); font-weight: 700; }
.attach-chip-gif img { height: 32px; border-radius: 4px; display: block; }

/* --- gif picker --- */
.gif-wrap { position: relative; flex-shrink: 0; }
.gif-panel {
  position: absolute; bottom: 46px; left: 0; width: 300px; max-height: 340px; background: #fff;
  border: 1px solid var(--line); border-radius: var(--radius-md); box-shadow: var(--shadow-2); padding: 10px;
  display: flex; flex-direction: column; gap: 8px; z-index: 40;
}
.gif-panel input {
  border: 1.5px solid var(--line); border-radius: 8px; padding: 7px 10px; font-size: 13.5px;
}
.gif-panel input:focus { outline: 2px solid var(--blue); outline-offset: -1px; }
.gif-results { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; overflow-y: auto; max-height: 260px; }
.gif-thumb { border: none; background: var(--paper-2); border-radius: 8px; padding: 0; overflow: hidden; cursor: pointer; }
.gif-thumb img { width: 100%; height: 84px; object-fit: cover; display: block; }
.gif-loading { grid-column: 1/-1; text-align: center; font-size: 12.5px; color: var(--text-soft); padding: 12px 0; }

/* --- member sidebar --- */
.member-sidebar { width: 224px; flex-shrink: 0; background: var(--ink-2); padding: 16px 14px; overflow-y: auto; }
.member-sidebar h3 { color: var(--text-inverse); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.member-row { display: flex; align-items: center; gap: 8px; padding: 5px 4px; border-radius: 8px; }
.member-row:hover { background: var(--ink-3); }
.member-avatar { width: 28px; height: 28px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; font-size: 11.5px; font-weight: 700; flex-shrink: 0; }
.member-name { font-size: 13.5px; color: var(--text-inverse); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-role { font-size: 10px; color: var(--text-inverse-soft); text-transform: uppercase; margin-left: auto; flex-shrink: 0; }
.member-manage-btn { background: none; border: none; color: var(--text-inverse-soft); cursor: pointer; font-size: 13px; padding: 3px; border-radius: 6px; flex-shrink: 0; }
.member-manage-btn:hover { color: #fff; background: var(--ink-3); }

/* --- thread panel --- */
.thread-panel {
  position: fixed; right: 0; top: 0; bottom: 0; width: 340px; background: var(--paper);
  border-left: 1px solid var(--line); display: flex; flex-direction: column; z-index: 30; box-shadow: var(--shadow-2);
}
.thread-header { height: 52px; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; border-bottom: 1px solid var(--line); font-weight: 700; background: #fff; }
.thread-parent { padding: 14px 16px; border-bottom: 1px solid var(--line); }
.thread-replies { flex: 1; overflow-y: auto; padding: 10px 16px; display: flex; flex-direction: column; gap: 10px; }

/* --- modals --- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(23,50,74,.5); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal { background: #fff; border-radius: var(--radius-lg); padding: 24px; width: 100%; max-width: 400px; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow-2); display: flex; flex-direction: column; gap: 14px; }
.modal h3 { font-size: 19px; color: var(--ink); }
.modal label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-soft); }
.modal input, .modal select { border: 1.5px solid var(--line); border-radius: var(--radius-sm); padding: 9px 11px; font-size: 14.5px; }
.modal input:focus, .modal select:focus { outline: 2px solid var(--blue); outline-offset: -1px; border-color: transparent; }
.optional { font-weight: 400; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.invite-code-display { font-family: 'Fraunces', serif; font-size: 28px; font-weight: 600; letter-spacing: .06em; text-align: center; padding: 14px; background: var(--paper-2); border-radius: 10px; color: var(--ink); }
.pinned-list { display: flex; flex-direction: column; gap: 10px; max-height: 320px; overflow-y: auto; }
.pinned-item { padding: 8px 10px; background: var(--paper-2); border-radius: 8px; font-size: 13px; }
.pinned-item b { display: block; font-size: 12.5px; margin-bottom: 2px; }

.settings-icon-row { display: flex; align-items: center; gap: 14px; }
.settings-icon-preview {
  width: 56px; height: 56px; border-radius: 50%; overflow: hidden; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; background: var(--paper-2);
  font-family: 'Fraunces', serif; font-weight: 700; font-size: 20px; color: var(--ink);
}
.settings-icon-preview img { width: 100%; height: 100%; object-fit: cover; }
.settings-icon-preview span { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: #fff; }
.settings-icon-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.color-swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.color-swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.color-swatch.selected { border-color: var(--ink); box-shadow: 0 0 0 2px #fff inset; }
.perm-check-list { display: flex; flex-direction: column; gap: 8px; margin-top: -6px; }
.perm-check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; font-weight: 500; color: var(--text); flex-direction: row !important; }
.perm-check input { width: auto; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 13.5px; z-index: 100; box-shadow: var(--shadow-2);
}

/* ===================== RESPONSIVE ===================== */
@media (max-width: 860px) {
  .member-sidebar { position: fixed; right: 0; top: 0; bottom: 0; z-index: 20; }
  .thread-panel { width: 100%; }
}
@media (max-width: 640px) {
  .rail { width: 60px; }
  .rail-item, .class-avatar { width: 42px; height: 42px; }
  .home-view { padding: 24px 16px; }
  .channel-sidebar { position: fixed; left: 60px; top: 0; bottom: 0; z-index: 15; transform: translateX(-110%); transition: transform .2s ease; }
  .channel-sidebar.open { transform: translateX(0); box-shadow: var(--shadow-2); }
  .gif-panel { width: 240px; }
}

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: rgba(23,50,74,.18); border-radius: 5px; }
.channel-sidebar ::-webkit-scrollbar-thumb, .member-sidebar ::-webkit-scrollbar-thumb, .rail::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); }
