/* === RESET & BASE === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --ink:     #1a1020;
  --parchment: #faf6ef;
  --cream:   #f2ead8;
  --gold:    #c9922a;
  --gold-light: #e8b94a;
  --plum:    #4a1942;
  --plum-light: #7a3070;
  --fog:     #e8e0d4;
  --mist:    #f7f3ec;
  --danger:  #c0392b;
  --success: #27ae60;
  --info:    #2980b9;
  --shadow:  0 4px 24px rgba(26,16,32,0.12);
  --radius:  14px;
  --sidebar-w: 270px;
  --topbar-h: 56px;
}
html, body { height: 100%; font-family: 'Inter', sans-serif; background: var(--parchment); color: var(--ink); }
.hidden { display: none !important; }

/* === MODAL OVERLAY === */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(26,16,32,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000; backdrop-filter: blur(4px);
}
.modal-overlay.active { display: flex; }
.modal-box {
  background: var(--parchment); border-radius: var(--radius);
  padding: 36px 32px; max-width: 480px; width: 90%;
  box-shadow: var(--shadow); position: relative;
  max-height: 90vh; overflow-y: auto;
}
.modal-box.small { max-width: 380px; }
.modal-box.large { max-width: 760px; width: 95%; }

/* === AUTH === */
.auth-box { text-align: center; }
.auth-logo { font-size: 48px; margin-bottom: 8px; }
.auth-title { font-family: 'Playfair Display', serif; font-size: 32px; color: var(--plum); margin-bottom: 4px; }
.auth-sub { color: var(--plum-light); font-size: 14px; margin-bottom: 24px; }
.auth-tabs { display: flex; gap: 0; margin-bottom: 20px; border: 2px solid var(--plum); border-radius: 8px; overflow: hidden; }
.auth-tab { flex: 1; padding: 10px; background: transparent; border: none; cursor: pointer; font-size: 14px; font-weight: 500; color: var(--plum); transition: all .2s; }
.auth-tab.active { background: var(--plum); color: #fff; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-hint { font-size: 13px; color: #888; margin-top: 4px; }
.auth-hint a { color: var(--gold); text-decoration: none; font-weight: 600; }

/* === INPUTS === */
.inp {
  width: 100%; padding: 11px 14px; border: 1.5px solid var(--fog);
  border-radius: 9px; font-size: 14px; font-family: inherit;
  background: #fff; color: var(--ink); transition: border-color .2s; outline: none;
}
.inp:focus { border-color: var(--plum); }
textarea.inp { resize: vertical; min-height: 72px; }
select.inp { appearance: none; cursor: pointer; }

/* === BUTTONS === */
.btn-primary {
  padding: 11px 20px; background: var(--plum); color: #fff;
  border: none; border-radius: 9px; font-size: 14px; font-weight: 600;
  cursor: pointer; transition: background .2s; font-family: inherit;
}
.btn-primary:hover { background: var(--plum-light); }
.btn-primary.small { padding: 7px 14px; font-size: 13px; }
.btn-secondary {
  padding: 11px 20px; background: var(--fog); color: var(--ink);
  border: none; border-radius: 9px; font-size: 14px; font-weight: 500;
  cursor: pointer; transition: background .2s; font-family: inherit;
}
.btn-secondary:hover { background: var(--cream); }
.btn-danger {
  padding: 11px 20px; background: var(--danger); color: #fff;
  border: none; border-radius: 9px; font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: inherit;
}
.btn-danger:hover { background: #a93226; }
.btn-google {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 11px 20px; background: #fff; color: var(--ink);
  border: 1.5px solid var(--fog); border-radius: 9px; font-size: 14px;
  font-weight: 500; cursor: pointer; transition: border-color .2s; font-family: inherit;
}
.btn-google:hover { border-color: var(--plum); }
.btn-icon {
  background: transparent; border: none; cursor: pointer; font-size: 18px;
  padding: 6px; border-radius: 7px; transition: background .15s;
  display: inline-flex; align-items: center; justify-content: center;
}
.btn-icon:hover { background: var(--fog); }
.btn-send {
  background: var(--gold); color: #fff; border: none; border-radius: 9px;
  padding: 10px 16px; font-size: 18px; cursor: pointer; transition: background .2s;
  align-self: flex-end;
}
.btn-send:hover { background: var(--gold-light); }
.divider { display: flex; align-items: center; gap: 10px; color: #aaa; font-size: 12px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--fog); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* === APP LAYOUT === */
#app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  height: var(--topbar-h); background: var(--plum); color: #fff;
  padding: 0 16px; position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  box-shadow: 0 2px 12px rgba(0,0,0,0.2);
}
.topbar-title { flex: 1; font-family: 'Playfair Display', serif; font-size: 18px; }
.hamburger { background: transparent; border: none; color: #fff; font-size: 22px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.hamburger:hover { background: rgba(255,255,255,0.12); }
.admin-btn { background: transparent; border: none; color: var(--gold-light); font-size: 22px; cursor: pointer; }

/* === SIDEBAR === */
.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0; width: var(--sidebar-w);
  background: var(--plum); color: #fff; display: flex; flex-direction: column;
  z-index: 200; transform: translateX(-100%); transition: transform .28s cubic-bezier(.4,0,.2,1);
  overflow-y: auto;
}
.sidebar.open { transform: translateX(0); }
.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 16px 12px; border-bottom: 1px solid rgba(255,255,255,0.1); }
.logo { font-family: 'Playfair Display', serif; font-size: 20px; font-weight: 700; }
.user-card { display: flex; align-items: center; gap: 10px; padding: 16px; border-bottom: 1px solid rgba(255,255,255,0.1); }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--gold); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 16px; font-weight: 700; flex-shrink: 0; }
.user-info { flex: 1; min-width: 0; }
.user-name { display: block; font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-type { font-size: 11px; color: rgba(255,255,255,0.6); text-transform: uppercase; letter-spacing: .5px; }
.nav-menu { flex: 1; padding: 12px 10px; display: flex; flex-direction: column; gap: 3px; }
.nav-item {
  width: 100%; text-align: left; background: transparent; border: none;
  color: rgba(255,255,255,0.8); padding: 11px 14px; border-radius: 9px;
  font-size: 14px; cursor: pointer; font-family: inherit; transition: all .15s;
}
.nav-item:hover { background: rgba(255,255,255,0.1); color: #fff; }
.nav-item.active { background: var(--gold); color: #fff; }
.nav-item.danger { color: #ff8888; }
.nav-item.danger:hover { background: rgba(192,57,43,0.3); color: #ffaaaa; }
.sidebar-footer { padding: 12px 10px; border-top: 1px solid rgba(255,255,255,0.1); }

/* === MAIN CONTENT === */
.main-content { margin-top: var(--topbar-h); flex: 1; overflow: hidden; display: flex; flex-direction: column; }
.section { flex: 1; display: flex; flex-direction: column; overflow: hidden; padding: 24px 20px 0; }
.section.hidden { display: none; }
.section-header { margin-bottom: 20px; }
.section-header h2 { font-family: 'Playfair Display', serif; font-size: 24px; color: var(--plum); }
.section-header p { color: #888; font-size: 14px; margin-top: 4px; }
.section-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; }
.section-header > div { display: flex; flex-direction: column; }

/* === CHAT === */
.chat-window {
  flex: 1; overflow-y: auto; padding: 8px 0 16px;
  display: flex; flex-direction: column; gap: 14px;
}
.chat-bubble { display: flex; gap: 10px; max-width: 80%; }
.chat-bubble.ai { align-self: flex-start; }
.chat-bubble.user { align-self: flex-end; flex-direction: row-reverse; }
.bubble-avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--plum); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; margin-top: 2px; }
.chat-bubble.user .bubble-avatar { background: var(--gold); }
.bubble-content { background: #fff; border-radius: 16px; padding: 12px 16px; font-size: 14px; line-height: 1.6; box-shadow: 0 2px 8px rgba(0,0,0,0.07); max-width: 100%; }
.chat-bubble.user .bubble-content { background: var(--plum); color: #fff; }
.bubble-content img { max-width: 240px; border-radius: 8px; margin-top: 8px; display: block; }
.bubble-content video { max-width: 280px; border-radius: 8px; margin-top: 8px; }
.bubble-content p { margin: 0; }
.bubble-content p + p { margin-top: 8px; }
.typing-dots { display: flex; gap: 4px; padding: 4px 2px; }
.typing-dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--plum-light); animation: blink 1.2s infinite; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100%{opacity:.2} 40%{opacity:1} }

.chat-input-area { padding: 12px 0 20px; background: var(--parchment); }
.chat-input-row { display: flex; gap: 8px; align-items: flex-end; }
.chat-textarea {
  flex: 1; resize: none; border: 1.5px solid var(--fog); border-radius: 12px;
  padding: 10px 14px; font-family: inherit; font-size: 14px; color: var(--ink);
  background: #fff; outline: none; max-height: 140px; overflow-y: auto; transition: border-color .2s;
}
.chat-textarea:focus { border-color: var(--plum); }
.media-preview { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.media-thumb { position: relative; }
.media-thumb img, .media-thumb video { width: 70px; height: 70px; object-fit: cover; border-radius: 8px; }
.media-thumb .remove-media { position: absolute; top: -6px; right: -6px; background: var(--danger); color: #fff; border: none; border-radius: 50%; width: 18px; height: 18px; cursor: pointer; font-size: 11px; display: flex; align-items: center; justify-content: center; }

/* === GROUPS === */
.groups-layout { display: flex; flex: 1; gap: 0; overflow: hidden; border: 1.5px solid var(--fog); border-radius: var(--radius); background: #fff; }
.groups-list { width: 220px; border-right: 1.5px solid var(--fog); overflow-y: auto; flex-shrink: 0; }
.group-item { padding: 14px 16px; cursor: pointer; border-bottom: 1px solid var(--fog); transition: background .15s; }
.group-item:hover { background: var(--mist); }
.group-item.active { background: var(--plum); color: #fff; }
.group-item-name { font-weight: 600; font-size: 14px; }
.group-item-desc { font-size: 12px; opacity: .7; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.group-chat-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.group-messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.group-input-area { padding: 12px 16px; border-top: 1.5px solid var(--fog); }
.group-input-row { display: flex; gap: 8px; align-items: flex-end; }
.group-msg { display: flex; gap: 8px; }
.group-msg.own { flex-direction: row-reverse; }
.group-msg-content { background: var(--mist); border-radius: 12px; padding: 10px 14px; font-size: 14px; max-width: 70%; }
.group-msg.own .group-msg-content { background: var(--plum); color: #fff; }
.group-msg-author { font-size: 11px; font-weight: 600; margin-bottom: 3px; color: var(--plum); }
.group-msg.own .group-msg-author { color: rgba(255,255,255,0.7); text-align: right; }
.group-msg-text { line-height: 1.5; }
.group-msg-img { max-width: 200px; border-radius: 8px; margin-top: 6px; display: block; }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: #aaa; font-size: 14px; gap: 10px; }
.empty-state span { font-size: 40px; }

/* === STORIES GRID === */
.stories-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 18px; overflow-y: auto; padding-bottom: 24px; }
.story-card { background: #fff; border-radius: var(--radius); padding: 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.06); cursor: pointer; transition: transform .15s, box-shadow .15s; border: 1.5px solid var(--fog); }
.story-card:hover { transform: translateY(-3px); box-shadow: 0 6px 20px rgba(0,0,0,0.1); }
.story-card-title { font-family: 'Playfair Display', serif; font-size: 18px; color: var(--plum); margin-bottom: 8px; }
.story-card-preview { font-size: 13px; color: #666; line-height: 1.6; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.story-card-meta { font-size: 11px; color: #aaa; margin-top: 12px; }

/* === STORY MODAL === */
.story-modal-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 16px; gap: 16px; }
.story-modal-header h3 { font-family: 'Playfair Display', serif; font-size: 22px; color: var(--plum); }
.story-modal-body { font-size: 15px; line-height: 1.8; color: var(--ink); white-space: pre-wrap; max-height: 60vh; overflow-y: auto; border-top: 1.5px solid var(--fog); padding-top: 16px; }

/* === ADMIN === */
.admin-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.admin-header h3 { font-family: 'Playfair Display', serif; font-size: 22px; color: var(--plum); }
.admin-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 20px; }
.admin-tab-btn {
  padding: 8px 16px; border-radius: 8px; border: 1.5px solid var(--fog);
  background: transparent; cursor: pointer; font-size: 13px; font-family: inherit; font-weight: 500; transition: all .15s;
}
.admin-tab-btn.active { background: var(--plum); color: #fff; border-color: var(--plum); }
.admin-content { min-height: 200px; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin-table th { text-align: left; padding: 10px 12px; background: var(--mist); color: var(--plum); font-weight: 600; border-bottom: 1.5px solid var(--fog); }
.admin-table td { padding: 10px 12px; border-bottom: 1px solid var(--fog); vertical-align: middle; }
.admin-table tr:hover td { background: var(--mist); }
.admin-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.tag.banned { background: #fde; color: var(--danger); }
.tag.active { background: #dfd; color: var(--success); }
.tag.google { background: #e8f4fd; color: var(--info); }
.tag.guest { background: var(--cream); color: var(--gold); }

/* === BAN === */
.ban-entry { background: var(--mist); border-radius: 10px; padding: 14px; margin-bottom: 10px; }
.ban-entry-name { font-weight: 600; color: var(--plum); }
.ban-entry-meta { font-size: 12px; color: #888; margin-top: 4px; }
.ban-entry-comment { font-size: 13px; margin-top: 8px; font-style: italic; }

/* === UPLOAD ZONE === */
.upload-zone {
  display: block; border: 2px dashed var(--fog); border-radius: 10px;
  padding: 24px; text-align: center; color: #aaa; cursor: pointer; transition: border-color .2s;
}
.upload-zone:hover { border-color: var(--plum); color: var(--plum); }
.upload-zone input { display: none; }
#addMediaPreview img, #addMediaPreview video { max-width: 100%; border-radius: 8px; margin-top: 10px; }

/* === BANNED OVERLAY === */
.banned-overlay {
  position: fixed; inset: 0; background: rgba(26,16,32,0.92);
  display: flex; align-items: center; justify-content: center; z-index: 9999;
}
.banned-box { background: #fff; border-radius: var(--radius); padding: 40px; text-align: center; max-width: 400px; }
.banned-box h2 { color: var(--danger); font-family: 'Playfair Display', serif; margin-bottom: 12px; }
.banned-box p { color: #555; font-size: 14px; line-height: 1.6; }

/* === RESPONSIVE === */
@media (max-width: 600px) {
  .groups-layout { flex-direction: column; }
  .groups-list { width: 100%; height: 140px; border-right: none; border-bottom: 1.5px solid var(--fog); }
  .modal-box { padding: 24px 16px; }
  .admin-table { font-size: 12px; }
  .admin-table th:nth-child(3), .admin-table td:nth-child(3) { display: none; }
}

/* =====================================================
   DARK MODE & APPEARANCE
   ===================================================== */

/* Dark mode variables */
[data-theme="dark"] {
  --ink:         #e8ddd0;
  --parchment:   #1a1420;
  --cream:       #231928;
  --gold:        #c9922a;
  --gold-light:  #e8b94a;
  --plum:        #9b5994;
  --plum-light:  #c07ab8;
  --fog:         #3a2e42;
  --mist:        #261d2e;
  --shadow:      0 4px 24px rgba(0,0,0,0.4);
}

[data-theme="dark"] body            { background: var(--parchment); color: var(--ink); }
[data-theme="dark"] .modal-box      { background: #231928; }
[data-theme="dark"] .inp            { background: #1a1420; color: var(--ink); border-color: var(--fog); }
[data-theme="dark"] .inp::placeholder { color: #6a5a78; }
[data-theme="dark"] .btn-secondary  { background: #3a2e42; color: var(--ink); }
[data-theme="dark"] .btn-secondary:hover { background: #4a3a54; }
[data-theme="dark"] .btn-google     { background: #231928; color: var(--ink); border-color: var(--fog); }
[data-theme="dark"] .topbar         { background: #120d18; }
[data-theme="dark"] .sidebar        { background: #120d18; }
[data-theme="dark"] .bubble-content { background: #2e2438; }
[data-theme="dark"] .story-card     { background: #231928; border-color: var(--fog); }
[data-theme="dark"] .story-card:hover { box-shadow: 0 6px 20px rgba(0,0,0,0.4); }
[data-theme="dark"] .group-chat-panel  { background: #1a1420; }
[data-theme="dark"] .groups-layout { background: #1a1420; border-color: var(--fog); }
[data-theme="dark"] .group-msg-content { background: #2e2438; }
[data-theme="dark"] .group-item:hover  { background: #2e2438; }
[data-theme="dark"] .group-item.active { background: var(--plum); }
[data-theme="dark"] .chat-textarea     { background: #1a1420; color: var(--ink); border-color: var(--fog); }
[data-theme="dark"] .admin-table th    { background: #2e2438; }
[data-theme="dark"] .admin-table tr:hover td { background: #2e2438; }
[data-theme="dark"] .ban-entry         { background: #2e2438; }
[data-theme="dark"] .appear-card       { background: #231928; border-color: var(--fog); }
[data-theme="dark"] .nav-item          { color: rgba(232,221,208,0.75); }
[data-theme="dark"] .nav-item:hover    { background: rgba(255,255,255,0.07); color: var(--ink); }
[data-theme="dark"] .auth-title        { color: var(--plum-light); }
[data-theme="dark"] .section-header h2 { color: var(--plum-light); }
[data-theme="dark"] .story-card-title  { color: var(--plum-light); }
[data-theme="dark"] .story-card-preview { color: #998888; }

/* Wallpaper overlay so text stays readable */
.main-content {
  position: relative;
}
.main-content::before {
  content: '';
  position: fixed;
  inset: 56px 0 0 0;
  background: inherit;
  pointer-events: none;
  z-index: -1;
}
.section {
  position: relative;
  z-index: 1;
}
/* Semi-transparent backing when wallpaper is set */
.has-wallpaper .section-header,
.has-wallpaper .chat-window,
.has-wallpaper .chat-input-area,
.has-wallpaper .groups-layout,
.has-wallpaper .stories-grid,
.has-wallpaper .appearance-body {
  background: rgba(255,255,255,0.82);
  backdrop-filter: blur(6px);
  border-radius: var(--radius);
  padding: 16px;
}
[data-theme="dark"] .has-wallpaper .section-header,
[data-theme="dark"] .has-wallpaper .chat-window,
[data-theme="dark"] .has-wallpaper .chat-input-area,
[data-theme="dark"] .has-wallpaper .groups-layout,
[data-theme="dark"] .has-wallpaper .stories-grid,
[data-theme="dark"] .has-wallpaper .appearance-body {
  background: rgba(20,10,28,0.82);
}

/* === APPEARANCE SECTION === */
.appearance-body {
  display: flex;
  flex-direction: column;
  gap: 24px;
  overflow-y: auto;
  padding-bottom: 32px;
}
.appear-card {
  background: #fff;
  border: 1.5px solid var(--fog);
  border-radius: var(--radius);
  padding: 22px 24px;
}
.appear-card h3 {
  font-family: 'Playfair Display', serif;
  font-size: 17px;
  color: var(--plum);
  margin-bottom: 16px;
}

/* Theme switcher */
.theme-switch-row {
  display: flex;
  gap: 12px;
}
.theme-btn {
  flex: 1;
  padding: 14px;
  border-radius: 10px;
  border: 2px solid var(--fog);
  background: transparent;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  font-weight: 500;
  color: var(--ink);
  transition: all .2s;
}
.theme-btn.active {
  border-color: var(--plum);
  background: var(--plum);
  color: #fff;
}
.theme-btn:hover:not(.active) {
  border-color: var(--plum-light);
  background: var(--mist);
}

/* Wallpaper grid */
.wp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 10px;
}
.wp-thumb {
  border-radius: 10px;
  height: 72px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  cursor: pointer;
  position: relative;
  border: 3px solid transparent;
  transition: border-color .2s, transform .15s;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}
.wp-thumb:hover { transform: scale(1.04); }
.wp-thumb.selected { border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold); }
.wp-thumb > span { font-size: 22px; color: #aaa; }
.wp-label {
  width: 100%;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 4px;
  background: rgba(0,0,0,0.45);
  color: #fff;
}
.wp-thumb[data-id="none"] .wp-label { background: rgba(0,0,0,0.1); color: var(--ink); }
