:root {
  --bg: #f8f9fa;
  --ink: #1f2933;
  --muted: #888;
  --panel: #ffffff;
  --line: #e2e8f0;
  --accent: #6366f1;
  --danger: #ef4444;
  --shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
  --border-radius: 8px;
}

[data-theme="dark"] {
  --bg: #111827;
  --ink: #f9fafb;
  --muted: #9ca3af;
  --panel: #1f2937;
  --line: #374151;
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
  background:
    repeating-linear-gradient(-45deg, transparent, transparent 10px, rgba(0,0,0,0.02) 10px, rgba(0,0,0,0.02) 20px),
    var(--bg);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex;
  flex-direction: column;
}

[data-theme="dark"] body {
  background:
    repeating-linear-gradient(-45deg, transparent, transparent 10px, rgba(255,255,255,0.02) 10px, rgba(255,255,255,0.02) 20px),
    var(--bg);
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Upload page: lock scroll */
body.upload-mode {
  height: 100vh;
  overflow: hidden;
}
body.upload-mode #app {
  height: 100vh;
  overflow: hidden;
}

button, input, select, textarea { font: inherit; }

button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 8px 16px;
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.2s, border-color 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
button:hover { background: var(--line); }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.primary:hover { background: #4f46e5; }
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
button.danger:hover { background: #dc2626; }
button.icon-btn { padding: 8px; border-radius: 50%; border: none; background: transparent; }
button.icon-btn:hover { background: rgba(0,0,0,0.06); }
[data-theme="dark"] button.icon-btn:hover { background: rgba(255,255,255,0.1); }
button:disabled { opacity: .5; cursor: not-allowed; }

/* ── Topbar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  position: relative;
  z-index: 50;
  flex-shrink: 0;
}
.brand {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: 0;
  max-width: calc(100% - 220px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.brand-title {
  font-weight: 700;
  font-size: 1.2rem;
}
.nav-left, .nav-right {
  display: flex;
  align-items: center;
  gap: 8px;
  position: relative;
}

/* ── Popovers ── */
.popover {
  position: absolute;
  top: calc(100% + 8px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 10px 25px -5px rgba(0,0,0,0.12), 0 8px 10px -6px rgba(0,0,0,0.08);
  display: none;
  z-index: 200;
}
.popover.active { display: block; }
.popover-left { left: 0; padding: 8px 0; min-width: 160px; }
.popover-right { right: 0; padding: 16px; width: 280px; max-width: calc(100vw - 32px); }

.menu-item {
  display: flex;
  align-items: center;
  padding: 10px 20px;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.15s;
  font-size: 0.95rem;
  gap: 8px;
}
.menu-item:hover { background: rgba(0,0,0,0.05); }
[data-theme="dark"] .menu-item:hover { background: rgba(255,255,255,0.06); }
.menu-item-disabled { opacity: 0.45; cursor: not-allowed; pointer-events: none; }

/* ── Layout shell ── */
.shell {
  flex: 1;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
}
body.upload-mode .shell { overflow: hidden; }

.upload-layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
}

/* ── Panel ── */
.panel {
  background: var(--panel);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 24px;
  border: 1px solid var(--line);
}
.panel h2 {
  margin: 0 0 16px;
  font-size: 1.15rem;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

/* ── Form helpers ── */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field span { font-size: 0.88rem; font-weight: 500; }

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  padding: 8px 12px;
  border-radius: 6px;
  outline: none;
  transition: border-color 0.2s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { min-height: 100px; resize: vertical; }

/* ── Drop zone ── */
.drop {
  width: 100%;
  max-width: 800px;
  height: 300px;
  flex-shrink: 0;
  margin-top: 4vh;
  border: 2px dashed #a5b4fc;
  background: rgba(255,255,255,0.55);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.25s, background 0.25s, box-shadow 0.25s;
}
[data-theme="dark"] .drop { background: rgba(31,41,55,0.55); border-color: #4f46e5; }
.drop:hover, .drop.dragover {
  background: rgba(255,255,255,0.85);
  border-color: var(--accent);
  box-shadow: 0 4px 24px rgba(99,102,241,0.12);
}
[data-theme="dark"] .drop:hover, [data-theme="dark"] .drop.dragover { background: rgba(31,41,55,0.85); }
.drop svg { width: 60px; height: 60px; margin-bottom: 14px; opacity: 0.75; }
.drop strong { font-size: 1.15rem; font-weight: 500; margin-bottom: 6px; }
.drop input { display: none; }

/* ── Toast message ── */
.message {
  position: fixed;
  top: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  min-width: 220px;
  max-width: 90vw;
  padding: 11px 20px;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0,0,0,0.12);
  display: none;
  text-align: center;
  font-size: 0.93rem;
  pointer-events: none;
}
.message.visible { display: block; }
.message.error { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
.message.ok    { background: #d1fae5; color: #065f46; border: 1px solid #6ee7b7; }
[data-theme="dark"] .message.error { background: #7f1d1d; color: #fecaca; border-color: #991b1b; }
[data-theme="dark"] .message.ok    { background: #064e3b; color: #a7f3d0; border-color: #065f46; }

/* Login-page inline message (not toast) */
.login-msg {
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 0.9rem;
  margin-top: 12px;
  display: none;
}
.login-msg.error { display: block; background: #fee2e2; color: #991b1b; }
[data-theme="dark"] .login-msg.error { background: #7f1d1d; color: #fecaca; }

/* ── Upload results ── */
.results-scroll {
  width: 100%;
  max-width: 800px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin-top: 20px;
  border-radius: 8px;
}
.results { display: grid; gap: 12px; }

/* ── Gallery ── */
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.item {
  border: 1px solid var(--line);
  background: var(--panel);
  padding: 14px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.item-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.item-head strong { font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item code {
  overflow-wrap: anywhere;
  background: var(--bg);
  padding: 7px 10px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.82rem;
}

.thumb {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 10;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg);
}
.thumb img { width: 100%; height: 100%; display: block; object-fit: cover; }
.thumb-empty::before {
  content: "No preview";
  display: grid;
  place-items: center;
  height: 100%;
  color: var(--muted);
  font-size: 0.82rem;
}
.thumb-empty img { display: none; }

.pill { border-radius: 99px; padding: 3px 10px; background: #fef08a; font-size: 0.78rem; color: #854d0e; white-space: nowrap; flex-shrink: 0; }

/* ── Footer ── */
.footer {
  text-align: center;
  padding: 14px;
  color: var(--muted);
  font-size: 0.82rem;
  background: #e5e7eb;
  flex-shrink: 0;
}
[data-theme="dark"] .footer { background: #1f2937; }

/* ── Settings ── */
.settings-group { border: 1px solid var(--line); padding: 16px; border-radius: 8px; margin-bottom: 14px; position: relative; }
.settings-group h3 { margin: 0 0 12px; font-size: 0.95rem; }
.delete-btn { position: absolute; top: 14px; right: 14px; }

/* Directory chips */
.directory-rows { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.directory-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  min-width: 120px;
}
.dir-info { display: flex; align-items: center; flex: 1; min-width: 0; }
.dir-name {
  font-size: 0.9rem;
  font-weight: 600;
  outline: none;
  border-radius: 4px;
  padding: 0 2px;
}
.dir-name[contenteditable="true"]:focus {
  background: var(--panel);
  box-shadow: 0 0 0 2px var(--accent);
}
.dir-del {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0 4px;
  border-radius: 4px;
  line-height: 1;
  flex-shrink: 0;
}
.dir-del:hover { background: #fee2e2; color: var(--danger); }



/* ── Responsive ── */
@media (max-width: 720px) {
  .topbar { padding: 10px 16px; }
  .brand { position: static; transform: none; max-width: 44vw; }
  .shell { padding: 14px; }
  .drop { height: 240px; margin-top: 2vh; }
  .drop svg { width: 44px; height: 44px; }
  .drop strong { font-size: 1rem; }
  .panel { padding: 16px; }
  .files { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
  .popover-right { right: -8px; }
}
