:root {
  --bg-color: #eef2f7;
  --surface: #ffffff;
  --surface-muted: #f7f9fc;
  --text-color: #172033;
  --text-muted: #68748a;
  --sidebar-bg: #f8fafc;
  --sidebar-border: #dfe5ee;
  --editor-chrome-bg: #2d2d2d;
  --editor-chrome-text: #eeeeee;
  --status-saved: #4caf50;
  --status-saving: #ff9800;
  --status-error: #f44336;
  --focus-ring: #246bfe;
  --hover-bg: #eaf1ff;
  --border-color: #dfe5ee;
  --primary-color: #246bfe;
  --primary-hover: #1557d5;
  --error-color: #d9363e;
  --editor-bg: #ffffff;
  --radius-sm: 6px;
  --radius-md: 10px;
  --shadow-soft: 0 8px 28px rgba(23, 32, 51, 0.08);
}

* { box-sizing: border-box; }
.upload-guidance { display: flex; flex-wrap: wrap; gap: 6px 20px; justify-content: space-between; padding: 9px 16px; font-size: 12px; line-height: 1.5; color: #49566b; background: var(--surface-muted); border-bottom: 1px solid var(--border-color); }
#share-status { font-weight: 600; }
.folder-header { flex-wrap: wrap; gap: 3px; }
.folder-header .folder-name { width: 100%; }
.folder-name > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-header .folder-actions { flex-wrap: wrap; gap: 2px !important; }
button:disabled, input:disabled { opacity: 0.6; cursor: wait; }
body { margin: 0; font-family: Inter, "Segoe UI", "PingFang SC", system-ui, sans-serif; display: flex; flex-direction: column; height: 100vh; overflow: hidden; background: var(--bg-color); color: var(--text-color); }
button, input, select { font: inherit; }
button:focus-visible, label.btn:focus-within, input:focus-visible, select:focus-visible, .file-item:focus-visible, .folder-name:focus-visible { outline: 3px solid rgba(36, 107, 254, 0.28); outline-offset: 2px; }

/* --- App Layout --- */
.app-layout {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

/* --- Top Toolbar --- */
.top-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 1rem;
  background-color: rgba(255,255,255,0.96);
  border-bottom: 1px solid var(--border-color);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  min-height: 64px;
  flex-shrink: 0;
}

.toolbar-left, .toolbar-right, .toolbar-center {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.toolbar-left h2 {
  font-size: 1.25rem;
  font-weight: 750;
  margin: 0;
  color: var(--text-color);
  border-right: 1px solid var(--border-color);
  padding-right: 1rem;
}

.editor-title {
  font-size: 0.9rem;
  color: var(--text-muted);
  font-weight: 500;
}

.top-toolbar button, .top-toolbar label.btn {
  background-color: #fff;
  border: 1px solid var(--border-color);
  color: var(--text-color);
  padding: 0.55rem 0.85rem;
  min-height: 38px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, transform 0.15s;
  display: inline-block;
}

.top-toolbar button:hover, .top-toolbar label.btn:hover {
  background-color: var(--surface-muted);
  border-color: #b9c5d6;
}
.top-toolbar button:active, .top-toolbar label.btn:active { transform: translateY(1px); }
#btn-new-file { background: var(--primary-color); border-color: var(--primary-color); color: white; }
#btn-new-file:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

.top-toolbar button.danger {
  color: var(--error-color);
  border-color: #fca5a5;
}
.top-toolbar button.danger:hover {
  background-color: #fef2f2;
}

/* --- Workspace Container (3-panes) --- */
.workspace-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  gap: 0;
  padding: 10px;
}

/* Sidebar */
.sidebar {
  width: 240px;
  min-width: 190px;
  background-color: var(--sidebar-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md) 0 0 var(--radius-md);
  display: flex;
  flex-direction: column;
}

.pane-header {
  min-height: 54px;
  padding: 0.65rem 0.8rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  background: var(--surface-muted);
}
.pane-header h3 { margin: 0; font-size: 0.95rem; line-height: 1.2; }
.pane-kicker { display: block; color: var(--text-muted); font-size: 0.65rem; font-weight: 700; letter-spacing: 0.08em; margin-bottom: 2px; }
.pane-meta { color: var(--text-muted); font-size: 0.72rem; }
.sidebar-header #btn-new-folder { width: 32px; height: 32px; border: 1px solid var(--border-color); background: var(--surface); border-radius: var(--radius-sm); cursor: pointer; color: var(--primary-color); font-size: 1.1rem; }

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 0.65rem;
}
.folder-item { margin-bottom: 0.45rem; }
.folder-name { min-width: 0; font-weight: 650; cursor: pointer; padding: 0.4rem 0.45rem; border-radius: var(--radius-sm); }
.folder-name:hover { background: var(--hover-bg); }
.folder-actions, .file-actions { display: flex; align-items: center; gap: 0.25rem; }
.folder-actions button, .file-actions button { border: 0; background: transparent; color: var(--text-muted); cursor: pointer; padding: 0.3rem 0.4rem; border-radius: var(--radius-sm); }
.folder-actions button:hover, .file-actions button:hover { background: #e2e8f0; color: #0f172a; }
.folder-actions button.danger-text:hover { color: #b91c1c; background: #fee2e2; }
.file-list { list-style: none; padding-left: 1rem; margin: 0.25rem 0 0 0; }
.file-item { min-height: 36px; padding: 0.35rem 0.5rem; cursor: pointer; border-radius: var(--radius-sm); display: flex; align-items: center; justify-content: space-between; gap: 0.4rem; }
.file-item > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-item:hover, .file-item.active { background: var(--hover-bg); }

/* Editor Pane */
.editor-pane {
  flex: 1;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  background-color: var(--editor-bg);
  border-top: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
}

.CodeMirror {
  height: 100% !important;
  font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
  font-size: 14px;
}

/* Preview Pane */
.preview-pane {
  flex: 1;
  width: 40%;
  min-width: 200px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border-color);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  overflow: hidden;
}

#preview-iframe {
  flex: 1;
  width: 100%;
  border: none;
}

/* Resizer */
.resizer {
  width: 6px;
  background-color: var(--bg-color);
  cursor: col-resize;
  transition: background-color 0.2s;
}
.resizer:hover, .resizer.dragging {
  background-color: var(--primary-color);
}

.editor-status { font-size: 0.9rem; display: flex; align-items: center; gap: 0.5rem; }
.status-indicator { width: 8px; height: 8px; border-radius: 50%; background: #ccc; }
.status-indicator.saved { background: var(--status-saved); }
.status-indicator.saving { background: var(--status-saving); }
.status-indicator.error { background: var(--status-error); }
.editor-container { flex: 1; overflow: hidden; position: relative; }
.empty-state { margin: auto; padding: 1.5rem; max-width: 260px; text-align: center; color: var(--text-muted); display: flex; flex-direction: column; align-items: center; gap: 0.45rem; }
.empty-state[hidden] { display: none !important; }
.empty-state strong { color: var(--text-color); font-size: 0.95rem; }
.empty-state span { font-size: 0.82rem; line-height: 1.55; }
.empty-actions { display: flex; gap: 0.5rem; margin-top: 0.55rem; }
.empty-actions button { border: 1px solid var(--primary-color); background: var(--primary-color); color: white; padding: 0.5rem 0.7rem; border-radius: var(--radius-sm); cursor: pointer; }
.empty-actions button.secondary { color: var(--primary-color); background: white; }
.pane-empty { position: absolute; inset: 54px 0 0; justify-content: center; z-index: 2; background: var(--surface); }
.editor-pane, .preview-pane { position: relative; }

/* Login Overlay */
.login-overlay { position: fixed; inset: 0; background: linear-gradient(135deg, #f8fafc, #eaf1ff); display: flex; justify-content: center; align-items: center; z-index: 1000; }
.login-box { background: white; padding: 2rem; border: 1px solid var(--border-color); border-radius: 14px; box-shadow: var(--shadow-soft); width: 100%; max-width: 400px; }
.login-box h1 { margin-top: 0; text-align: center; }
.login-form { display: flex; flex-direction: column; gap: 1rem; }
.login-form input { padding: 0.5rem; border: 1px solid #ccc; border-radius: 4px; }
.login-form input:focus { outline: none; border-color: var(--focus-ring); box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.2); }
.login-form button { padding: 0.5rem; background: var(--focus-ring); color: white; border: none; border-radius: 4px; cursor: pointer; font-weight: bold; }
.login-form button:hover { background: #1976d2; }
.login-error { color: var(--status-error); font-size: 0.9rem; text-align: center; min-height: 1.2em; }

/* Responsive */
@media (min-width: 601px) and (max-width: 900px) {
  .top-toolbar { align-items: flex-start; flex-wrap: wrap; }
  .toolbar-right { flex: 1 1 100%; justify-content: flex-end; }
  .workspace-container { padding: 8px; }
  .sidebar { width: 190px; min-width: 190px; }
  .top-toolbar button, .top-toolbar label.btn { padding-inline: 0.65rem; }
}

@media (max-width: 600px) {
  body { height: 100dvh; }
  .app-layout { flex-direction: column; }
  .top-toolbar {
    height: auto;
    padding: 0.55rem 0.65rem 0.65rem;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0.5rem;
  }
  .toolbar-left { min-width: 0; gap: 0.5rem; }
  .toolbar-left h2 { font-size: 1rem; padding-right: 0.5rem; white-space: nowrap; }
  .editor-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .toolbar-center { justify-self: end; }
  .toolbar-right {
    grid-column: 1 / -1;
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.4rem;
  }
  .top-toolbar .toolbar-right button,
  .top-toolbar .toolbar-right label.btn {
    width: 100%;
    min-width: 0;
    padding: 0.48rem 0.25rem;
    text-align: center;
    white-space: nowrap;
    font-size: 0.78rem;
  }
  .workspace-container { flex-direction: column; min-height: 0; }
  .workspace-container { padding: 0; }
  .sidebar { width: 100%; min-width: 0; height: 30%; border: 0; border-radius: 0; border-bottom: 1px solid var(--border-color); }
  .sidebar-header { padding: 0.55rem 0.75rem; }
  .sidebar-content { padding: 0.35rem 0.5rem; }
  .folder-actions { gap: 0.1rem !important; }
  .folder-actions button, .file-actions button, .folder-actions label { min-width: 40px; min-height: 40px; display: inline-flex; align-items: center; justify-content: center; font-size: 0.72rem; }
  .file-actions { display: flex !important; }
  .editor-pane { height: 35%; min-height: 0; min-width: 0; border: 0; border-bottom: 1px solid var(--border-color); }
  .preview-pane { height: 35%; min-height: 0; min-width: 0; width: 100%; border: 0; border-radius: 0; }
  .resizer { display: none; }
  .login-box { width: calc(100% - 2rem); }
}

/* --- Custom Dialogs --- */
.custom-dialog-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}
.custom-dialog-box {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
  width: 90%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.custom-dialog-message {
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-color);
}
.custom-dialog-input {
  padding: 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 1rem;
  width: 100%;
}
.custom-dialog-select {
  padding: 0.55rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 1rem;
  width: 100%;
  background: white;
}
.custom-dialog-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(33, 150, 243, 0.2);
}
.custom-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.5rem;
}
.custom-dialog-btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
}
.custom-dialog-btn.cancel {
  background: transparent;
  color: #666;
}
.custom-dialog-btn.cancel:hover {
  background: #f5f5f5;
}
.custom-dialog-btn.confirm {
  background: var(--primary-color);
  color: white;
}
.custom-dialog-btn.confirm:hover {
  background: #1976d2;
}
