:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #f0f1f6;
  --border: #e2e4ec;
  --text: #1b1e28;
  --text-dim: #6b7080;
  --accent: #5b5bf6;
  --accent-hover: #4747e0;
  --accent-soft: #eef0ff;
  --danger: #e5484d;
  --shadow: 0 4px 20px rgba(20, 20, 40, 0.08);
  --radius: 10px;
  --font-ui: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

html[data-theme="dark"] {
  --bg: #14151c;
  --surface: #1b1d27;
  --surface-2: #23252f;
  --border: #2e3140;
  --text: #eceef4;
  --text-dim: #9296a8;
  --accent: #8484ff;
  --accent-hover: #9b9bff;
  --accent-soft: #262850;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
}

#app { display: flex; flex-direction: column; height: 100vh; }
.hidden { display: none !important; }

/* ===== Topbar ===== */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 12px;
  z-index: 50;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 10px; }
.brand { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.icon-btn {
  border: none; background: transparent; cursor: pointer; font-size: 18px;
  width: 34px; height: 34px; border-radius: 8px; color: var(--text);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); }
.filename-input {
  border: 1px solid transparent; background: transparent; font-size: 14px; font-weight: 600;
  padding: 6px 8px; border-radius: 6px; color: var(--text); width: 180px;
}
.filename-input:hover, .filename-input:focus { border-color: var(--border); background: var(--surface-2); outline: none; }
.save-status { font-size: 12px; color: var(--text-dim); white-space: nowrap; }
.save-status.saved { color: #2fb380; }
.save-status.saving { color: var(--accent); }

.mode-switch { display: flex; background: var(--surface-2); border-radius: 8px; padding: 3px; }
.mode-btn {
  border: none; background: transparent; padding: 6px 12px; border-radius: 6px; cursor: pointer;
  font-size: 13px; font-weight: 600; color: var(--text-dim);
}
.mode-btn.active { background: var(--accent); color: #fff; }
.mode-btn.disabled { opacity: .45; pointer-events: none; }

.btn-google {
  display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border);
  padding: 7px 14px; border-radius: 8px; font-weight: 600; font-size: 13px; text-decoration: none; color: var(--text);
}
.btn-google:hover { border-color: var(--accent); }
.btn-google.large { padding: 12px 20px; font-size: 14px; justify-content: center; }
.badge-local { font-size: 12px; padding: 5px 10px; border-radius: 20px; background: var(--surface-2); color: var(--text-dim); }

.user-menu { position: relative; }
.user-chip { display: flex; align-items: center; gap: 8px; border: none; background: var(--surface-2); border-radius: 20px; padding: 4px 12px 4px 4px; cursor: pointer; }
.user-chip img { width: 26px; height: 26px; border-radius: 50%; }
.user-chip span { font-size: 13px; font-weight: 600; color: var(--text); max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow); min-width: 200px; padding: 6px; z-index: 100;
}
.dropdown-item, a.dropdown-item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; border: none; background: none;
  padding: 9px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--text); text-decoration: none;
}
.dropdown-item:hover { background: var(--surface-2); }
.dropdown-item-static { padding: 8px 10px; font-size: 12px; color: var(--text-dim); border-bottom: 1px solid var(--border); margin-bottom: 4px; }

/* ===== Body layout ===== */
.body-wrap { flex: 1; display: flex; min-height: 0; }

.sidebar {
  width: 260px; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; transition: margin-left .2s ease; overflow-y: auto;
}
.sidebar.collapsed { margin-left: -260px; }
.sidebar-section { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.sidebar-section.grow { flex: 1; overflow-y: auto; }
.sidebar-title { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-dim); font-weight: 700; margin-bottom: 10px; }
.sidebar-note { font-size: 12.5px; color: var(--text-dim); line-height: 1.5; margin: 0 0 10px; }

.side-btn {
  display: block; width: 100%; text-align: center; padding: 9px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 600; font-size: 13px; cursor: pointer; text-decoration: none;
}
.side-btn:hover { border-color: var(--accent); color: var(--accent); }
.side-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.side-btn.primary:hover { background: var(--accent-hover); }

.file-list { display: flex; flex-direction: column; gap: 4px; }
.file-item { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.file-item:hover { background: var(--surface-2); }
.file-item.active { background: var(--accent-soft); }
.file-item .fname { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-item .fmeta { font-size: 10px; color: var(--text-dim); }
.file-item .fdel, .file-item .fren { opacity: 0; border: none; background: none; cursor: pointer; font-size: 13px; }
.file-item .fdel { color: var(--danger); }
.file-item .fren { color: var(--text-dim); }
.file-item:hover .fdel, .file-item:hover .fren { opacity: 1; }
.file-item .fshare { font-size: 10px; }
.empty-hint { font-size: 12.5px; color: var(--text-dim); padding: 6px 2px; }

.stat-row { display: flex; justify-content: space-between; font-size: 12.5px; padding: 3px 0; color: var(--text-dim); }
.stat-row b { color: var(--text); }

/* ===== Editor Area ===== */
.editor-area { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }

.rich-toolbar, .code-toolbar, .app-toolbar {
  background: var(--surface); border-bottom: 1px solid var(--border); padding: 6px 12px;
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap;
}
.code-toolbar { gap: 10px; }
.app-toolbar { gap: 8px; }
.app-toolbar .spacer { flex: 1; }

.tool-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 7px;
  padding: 6px 12px; font-size: 12.5px; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 6px;
}
.tool-btn:hover { border-color: var(--accent); color: var(--accent); }
.tool-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.tool-btn.primary:hover { background: var(--accent-hover); }
.tool-check { font-size: 12.5px; color: var(--text-dim); display: flex; align-items: center; gap: 5px; }

#langSelect, #cmTheme {
  padding: 6px 10px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 12.5px; font-weight: 600;
}

.dropdown-wrap { position: relative; }

.editor-wrap { flex: 1; min-height: 0; position: relative; overflow: hidden; }
#richEditorHost, #codeEditorHost { position: absolute; inset: 0; overflow: auto; }

#richEditorHost { background: var(--bg); padding: 24px 0; }
#richEditor {
  max-width: 850px; margin: 0 auto; background: var(--surface); min-height: 1000px;
  padding: 60px 70px; box-shadow: var(--shadow); border-radius: 4px; font-size: 15px; line-height: 1.7;
}
.ql-editor { font-family: var(--font-ui); }

#codeEditorHost .CodeMirror { height: 100%; font-family: var(--font-mono); font-size: 14px; }

/* Quill toolbar theming */
.ql-toolbar.ql-snow, .ql-container.ql-snow { border: none !important; }
html[data-theme="dark"] .ql-snow .ql-stroke { stroke: var(--text-dim); }
html[data-theme="dark"] .ql-snow .ql-fill { fill: var(--text-dim); }
html[data-theme="dark"] .ql-snow .ql-picker { color: var(--text-dim); }

/* ===== Footer ===== */
.footer {
  display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap;
  padding: 10px; font-size: 12px; color: var(--text-dim); background: var(--surface); border-top: 1px solid var(--border);
}
.footer a { color: var(--text-dim); text-decoration: none; font-weight: 600; }
.footer a:hover { color: var(--accent); }
.footer-sep { opacity: .4; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10, 10, 20, 0.55); display: flex; align-items: center; justify-content: center; z-index: 999; backdrop-filter: blur(2px);
}
.modal {
  background: var(--surface); border-radius: 16px; padding: 32px; max-width: 440px; width: 92%; text-align: center;
  box-shadow: var(--shadow); border: 1px solid var(--border);
}
.modal-icon { font-size: 40px; margin-bottom: 8px; }
.modal h2 { margin: 0 0 10px; font-size: 19px; }
.modal p { color: var(--text-dim); font-size: 13.5px; line-height: 1.6; margin: 0 0 22px; }
.modal-actions { display: flex; flex-direction: column; gap: 10px; }

/* ===== Toast ===== */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 11px 20px; border-radius: 30px; font-size: 13px; font-weight: 600;
  box-shadow: var(--shadow); z-index: 1000; opacity: 0; transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; }

/* ===== Admin pages (shared) ===== */
.admin-wrap { max-width: 960px; margin: 40px auto; padding: 0 20px; }
.admin-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; margin-bottom: 20px; box-shadow: var(--shadow); }
.admin-card h2 { margin-top: 0; font-size: 17px; }
.admin-card h3 { font-size: 14px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; }
.form-row { margin-bottom: 16px; }
.form-row label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.form-row input, .form-row select {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13.5px;
}
.form-hint { font-size: 12px; color: var(--text-dim); margin-top: 5px; }
.admin-tabs { display: flex; gap: 4px; margin-bottom: 20px; border-bottom: 1px solid var(--border); }
.admin-tab { padding: 10px 16px; text-decoration: none; color: var(--text-dim); font-weight: 600; font-size: 13.5px; border-bottom: 2px solid transparent; }
.admin-tab.active { color: var(--accent); border-color: var(--accent); }
.alert { padding: 12px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 16px; }
.alert-error { background: #fdeaea; color: #c92a2a; }
.alert-success { background: #e6f9f0; color: #17915b; }
html[data-theme="dark"] .alert-error { background: #3a1f22; color: #ff8a8a; }
html[data-theme="dark"] .alert-success { background: #123a2b; color: #6fe3ac; }
table.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.admin-table th, table.admin-table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); }
table.admin-table th { color: var(--text-dim); font-weight: 600; font-size: 11.5px; text-transform: uppercase; }
.role-badge { padding: 3px 8px; border-radius: 20px; font-size: 11px; font-weight: 700; }
.role-badge.admin { background: var(--accent-soft); color: var(--accent); }
.role-badge.user { background: var(--surface-2); color: var(--text-dim); }
.btn-mini { border: 1px solid var(--border); background: var(--surface); padding: 5px 10px; border-radius: 6px; font-size: 11.5px; cursor: pointer; color: var(--text); text-decoration: none; }
.btn-mini.danger { color: var(--danger); border-color: #f3c1c3; }
.setup-wizard { max-width: 480px; margin: 60px auto; }
.center-logo { text-align: center; font-size: 42px; margin-bottom: 6px; }
.center-title { text-align: center; margin: 0 0 4px; }
.center-sub { text-align: center; color: var(--text-dim); font-size: 13px; margin-bottom: 24px; }
.btn-full { width: 100%; padding: 11px; border-radius: 8px; border: none; background: var(--accent); color: #fff; font-weight: 700; font-size: 14px; cursor: pointer; }
.btn-full:hover { background: var(--accent-hover); }
.link-back { display: inline-block; margin-top: 14px; font-size: 12.5px; color: var(--text-dim); text-decoration: none; }

@media (max-width: 780px) {
  .sidebar { position: fixed; left: 0; top: 49px; bottom: 0; z-index: 60; box-shadow: var(--shadow); }
  .sidebar.collapsed { margin-left: -280px; }
  .filename-input { width: 100px; }
  #richEditor { padding: 30px 24px; }
  .brand span { display: none; }
}
