:root {
  --navy: #1a3a6b;
  --navy-2: #1f4e78;
  --blue: #2563eb;
  --blue-hover: #1d4ed8;
  --blue-light: #eaf1fe;
  --bg: #f4f6f9;
  --card: #ffffff;
  --border: #dde3ec;
  --text: #16243b;
  --muted: #5d6b82;
  --danger: #c62828;
  --danger-light: #fdecec;
  --success: #1b7f4b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 36, 70, 0.06), 0 4px 16px rgba(16, 36, 70, 0.06);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select { font: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  height: 56px; display: flex; align-items: center; gap: 16px; padding: 0 20px;
  background: var(--navy); color: #fff; position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 16px; letter-spacing: 0.2px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--blue), #5b8def); font-size: 15px; font-weight: 800;
}
.brand small { font-weight: 500; opacity: 0.75; font-size: 13px; padding-left: 10px; border-left: 1px solid rgba(255,255,255,0.3); }
.topbar .spacer { flex: 1; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { color: rgba(255,255,255,0.8); padding: 6px 12px; border-radius: 6px; font-size: 14px; }
.topbar nav a:hover, .topbar nav a.active { background: rgba(255,255,255,0.12); color: #fff; text-decoration: none; }
.user-menu { position: relative; }
.user-btn {
  background: rgba(255,255,255,0.12); color: #fff; border: 0; border-radius: 20px; padding: 6px 12px 6px 6px;
  display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px;
}
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--blue); display: grid; place-items: center; font-size: 12px; font-weight: 700; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: #fff; color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 14px; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: #f7f9fc; text-decoration: none; }
.btn-primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-light); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.input, select.input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; background: #fff; font-size: 14px; color: var(--text);
}
.input:focus { outline: 2px solid rgba(37,99,235,0.25); border-color: var(--blue); }
label.field { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 12px 0 6px; }

/* ---------- Page layout ---------- */
.page { max-width: 1280px; margin: 0 auto; padding: 28px 24px 60px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head h1 { font-size: 22px; margin: 0; flex: 1; min-width: 200px; }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 18px; flex-wrap: wrap; }
.toolbar .search { max-width: 340px; }
.tabs { display: flex; gap: 4px; background: #e8edf4; padding: 4px; border-radius: 9px; }
.tabs button { border: 0; background: transparent; padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 14px; color: var(--muted); }
.tabs button.active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.08); font-weight: 600; }

/* ---------- Template grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 18px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; }
.thumb { height: 200px; background: #eef2f7; position: relative; overflow: hidden; cursor: pointer; border-bottom: 1px solid var(--border); }
.thumb iframe { width: 270.27%; height: 270.27%; border: 0; transform: scale(0.37); transform-origin: 0 0; pointer-events: none; background: #fff; }
.thumb .empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.card-body { padding: 12px 14px; flex: 1; }
.card-title { font-weight: 600; font-size: 15px; margin: 0 0 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { font-size: 12px; color: var(--muted); line-height: 1.5; }
.card-actions { display: flex; gap: 6px; padding: 0 14px 12px; flex-wrap: wrap; }
.empty-state { text-align: center; padding: 70px 20px; color: var(--muted); background: #fff; border: 1px dashed var(--border); border-radius: var(--radius); }
.empty-state h2 { color: var(--text); margin: 0 0 8px; font-size: 18px; }

/* ---------- Dropdown ---------- */
.dropdown { position: relative; display: inline-block; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(16,36,70,0.14); min-width: 200px; padding: 6px; z-index: 50; display: none;
}
.menu.open { display: block; }
.menu button, .menu a { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 14px; color: var(--text); }
.menu button:hover, .menu a:hover { background: var(--bg); text-decoration: none; }
.menu hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(10,25,50,0.45); display: grid; place-items: center; z-index: 100; padding: 20px; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 460px; padding: 22px; box-shadow: 0 20px 60px rgba(0,0,0,0.25); }
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.modal p { color: var(--muted); font-size: 14px; margin: 0 0 10px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* ---------- Toast ---------- */
#toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast { background: var(--text); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; box-shadow: var(--shadow); max-width: 380px; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* ---------- Tables (admin) ---------- */
.panel { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 20px; }
.panel h2 { margin: 0 0 4px; font-size: 17px; }
.panel .sub { color: var(--muted); font-size: 14px; margin: 0 0 16px; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px; padding: 8px 10px; border-bottom: 1px solid var(--border); }
table.data td { padding: 10px; border-bottom: 1px solid #eef1f6; vertical-align: middle; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 12px; font-weight: 600; background: var(--blue-light); color: var(--blue); }
.badge.off { background: #eee; color: #777; }
.badge.warn { background: #fff4e0; color: #a35d00; }
.badge.ok { background: #e6f5ec; color: var(--success); }
.update-box { border-radius: 8px; padding: 14px 16px; background: var(--bg); font-size: 14px; line-height: 1.6; }
.update-box code, .mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; background: #e8edf4; padding: 1px 6px; border-radius: 4px; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, var(--navy) 0%, var(--navy-2) 55%, #2b5c97 100%); }
.login-card { background: #fff; border-radius: 14px; padding: 32px; width: 100%; max-width: 380px; box-shadow: 0 24px 60px rgba(0,0,0,0.3); }
.login-card .brand { color: var(--navy); margin-bottom: 6px; }
.login-card .brand small { border-color: var(--border); color: var(--muted); }
.login-card h1 { font-size: 20px; margin: 18px 0 4px; }
.login-card p { color: var(--muted); font-size: 14px; margin: 0 0 8px; }
.error-msg { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* ---------- Editor page ---------- */
body.editor-page { overflow: hidden; display: flex; flex-direction: column; }
body.editor-page .topbar { position: static; flex: 0 0 auto; height: 48px; }
#editor { flex: 1; min-height: 0; }
.editor-actions { display: flex; gap: 6px; align-items: center; }
.editor-actions .btn { padding: 6px 10px; font-size: 13px; }
.loading { display: grid; place-items: center; height: 100%; color: var(--muted); }

.owner-notice { background: #fff4e0; color: #7a4500; font-size: 13px; font-weight: 600; padding: 4px 10px; border-radius: 6px; }
.owner-tag { display: inline-block; font-size: 11px; font-weight: 600; color: var(--navy-2); background: var(--blue-light); padding: 1px 7px; border-radius: 8px; margin-bottom: 4px; }

@media (max-width: 640px) {
  .topbar { gap: 10px; padding: 0 12px; }
  .brand small, .user-btn .user-name { display: none; }
  .user-btn { padding: 4px; }
  .topbar nav a { padding: 6px 8px; }
  .page { padding: 20px 14px 40px; }
  .toolbar .search { max-width: none; }
}

.user-btn .user-name { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { overflow: hidden; text-overflow: ellipsis; overflow-wrap: anywhere; }
.menu div { overflow-wrap: anywhere; }

.readonly-overlay { position: fixed; inset: 48px 0 0 0; background: rgba(244,246,249,0.92); z-index: 90; display: grid; place-items: center; padding: 20px; }
.readonly-card { background: #fff; border: 1px solid var(--border); border-radius: 12px; padding: 24px; max-width: 480px; box-shadow: var(--shadow); text-align: center; }
.readonly-card h2 { margin: 0 0 8px; font-size: 18px; }
.readonly-card p { color: var(--muted); }
