/* ============================================================================
   Tesseron - stylesheet. Light theme by default; dark via prefers-color-scheme
   and an explicit [data-theme] toggle.
   ========================================================================== */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e4e7ec;
  --border-strong: #d0d5dd;
  --text: #1a1c22;
  --text-muted: #667085;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --accent-contrast: #ffffff;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: #fee2e2;
  --ok: #16794c;
  --ok-soft: #dcfce7;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.10);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.18);
  --radius: 12px;
  --radius-sm: 8px;
}

/* Dark theme values, shared by the OS preference and the explicit toggle. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #1f232b;
    --border: #2a2f3a;
    --border-strong: #3a4150;
    --text: #e7eaf0;
    --text-muted: #97a0b0;
    --accent: #7c74ff;
    --accent-hover: #8f88ff;
    --accent-soft: #23233a;
    --accent-contrast: #ffffff;
    --danger: #f87171;
    --danger-hover: #ef4444;
    --danger-soft: #3a1e1e;
    --ok: #4ade80;
    --ok-soft: #16351f;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #181b21;
  --surface-2: #1f232b;
  --border: #2a2f3a;
  --border-strong: #3a4150;
  --text: #e7eaf0;
  --text-muted: #97a0b0;
  --accent: #7c74ff;
  --accent-hover: #8f88ff;
  --accent-soft: #23233a;
  --accent-contrast: #ffffff;
  --danger: #f87171;
  --danger-hover: #ef4444;
  --danger-soft: #3a1e1e;
  --ok: #4ade80;
  --ok-soft: #16351f;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

/* Author display rules (e.g. .modal { display: flex }) otherwise beat the UA
   [hidden] rule at equal specificity, leaving "hidden" elements visible. Force
   the intended semantics so toggling the hidden attribute always works. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body.modal-open { overflow: hidden; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.6rem; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }

.icon { display: block; flex: none; }
.muted { color: var(--text-muted); }

/* --- App layout ----------------------------------------------------------- */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 244px;
  flex: 0 0 244px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  z-index: 40;
}

.brand { display: flex; align-items: center; gap: 10px; color: var(--text); padding: 4px 8px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 9px;
  background: var(--accent); color: var(--accent-contrast);
}
.brand-mark svg { width: 20px; height: 20px; }
.brand-name { font-weight: 700; font-size: 1.15rem; letter-spacing: -0.01em; }

.side-nav { display: flex; flex-direction: column; gap: 2px; }
.side-link {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 11px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-weight: 500;
}
.side-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.side-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side-link .icon { color: currentColor; }

.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  height: 60px;
  display: flex; align-items: center; gap: 14px;
  padding: 0 22px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar .menu-toggle { display: none; }

.topsearch { position: relative; flex: 1; max-width: 420px; }
/* Qualified with [type="search"] so it beats the generic input rule further
   down (same specificity, later in the file), which would otherwise reset the
   pill padding and make the search icon overlap the text. */
.topsearch input[type="search"] {
  width: 100%; height: 38px;
  padding: 0 12px 0 36px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface-2); color: var(--text);
  font-size: 0.92rem;
}
.topsearch input:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.topsearch-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-muted); pointer-events: none; }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }

.content { flex: 1; width: 100%; max-width: 1120px; margin: 0 auto; padding: 26px 26px 60px; }

.app-footer {
  padding: 18px 26px; margin-top: auto;
  color: var(--text-muted); font-size: 0.85rem;
  border-top: 1px solid var(--border);
}

/* --- Theme switch --------------------------------------------------------- */
.theme-switch { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 3px; }
.switch-btn {
  border: none; background: transparent; color: var(--text-muted);
  font: inherit; font-size: 0.82rem; font-weight: 500;
  padding: 4px 11px; border-radius: 999px; cursor: pointer;
}
.switch-btn:hover { color: var(--text); }
.switch-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* --- Avatar + account dropdown ------------------------------------------- */
.avatar { display: inline-grid; place-items: center; border-radius: 50%; overflow: hidden; background: var(--accent); color: #fff; font-weight: 600; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-sm { width: 34px; height: 34px; font-size: 0.9rem; }
.avatar-md { width: 42px; height: 42px; font-size: 1.05rem; }

.user-menu { position: relative; }
.avatar-btn { border: none; background: none; padding: 0; cursor: pointer; border-radius: 50%; }
.avatar-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.user-dropdown {
  position: absolute; right: 0; top: calc(100% + 10px);
  min-width: 230px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 8px; z-index: 50;
}
.user-card { display: flex; align-items: center; gap: 10px; padding: 8px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
.user-card-text { display: flex; flex-direction: column; min-width: 0; }
.user-card-name { font-weight: 600; }
.user-card-sub { font-size: 0.82rem; color: var(--text-muted); }
.dropdown-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 9px 10px; border-radius: var(--radius-sm);
  color: var(--text); font: inherit; text-align: left;
}
.dropdown-item:hover { background: var(--surface-2); text-decoration: none; }
.dropdown-item-btn { border: none; background: none; cursor: pointer; }
.dropdown-form { margin: 0; }

.role-badge, .you-badge {
  display: inline-block; font-size: 0.68rem; font-weight: 600;
  padding: 1px 7px; border-radius: 999px; vertical-align: middle;
}
.role-badge { background: var(--accent-soft); color: var(--accent); }
.you-badge { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }

/* --- Page heads ----------------------------------------------------------- */
.page-head { margin-bottom: 22px; }
.page-head.with-action { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-sub { color: var(--text-muted); margin: 0; }
.section-title { margin: 30px 0 14px; font-size: 1.05rem; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* --- Buttons -------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  font: inherit; font-weight: 600; font-size: 0.9rem;
  padding: 9px 15px; border-radius: var(--radius-sm);
  border: 1px solid transparent; cursor: pointer; white-space: nowrap;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-block { width: 100%; justify-content: center; }
.btn:disabled { opacity: 0.6; cursor: default; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 34px; height: 34px; border-radius: var(--radius-sm);
  border: none; background: transparent; color: var(--text-muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* --- Cards ---------------------------------------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm);
  padding: 20px; margin-bottom: 18px;
}
.form-section-title { margin: 0 0 12px; font-size: 1.02rem; }
.subhead { margin: 20px 0 10px; font-size: 0.95rem; }
.hint { color: var(--text-muted); font-size: 0.88rem; margin: 0 0 14px; }

/* --- Stat + module cards (dashboard) ------------------------------------- */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 8px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 18px 20px; display: flex; flex-direction: column; gap: 4px; }
.stat-label { color: var(--text-muted); font-size: 0.85rem; font-weight: 500; }
.stat-value { font-size: 1.7rem; font-weight: 700; letter-spacing: -0.02em; }
.stat-sub { color: var(--text-muted); font-size: 0.8rem; }

.module-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.module-card {
  display: flex; align-items: center; gap: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm);
  padding: 16px 18px; color: var(--text);
}
.module-card:hover { text-decoration: none; border-color: var(--border-strong); box-shadow: var(--shadow-md); transform: translateY(-1px); transition: 0.15s; }
.module-icon { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 11px; background: var(--accent-soft); color: var(--accent); flex: none; }
.module-text { display: flex; flex-direction: column; min-width: 0; }
.module-title { font-weight: 600; }
.module-meta { color: var(--text-muted); font-size: 0.83rem; }

.recent-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.recent-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.recent-list li:last-child { border-bottom: none; }
.recent-list a { display: flex; align-items: center; gap: 10px; color: var(--text); min-width: 0; }
.recent-icon { color: var(--text-muted); flex: none; }
.recent-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-meta { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; flex: none; }

/* --- Tables --------------------------------------------------------------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow-sm); }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.data-table th { text-align: left; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); font-weight: 600; padding: 12px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.data-table td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-2); }
.cell-name { font-weight: 600; }
.col-actions { text-align: right; white-space: nowrap; width: 1%; }
.col-actions .icon-btn { width: 30px; height: 30px; }
.doc-chip { display: inline-grid; place-items: center; margin-left: 6px; color: var(--text-muted); vertical-align: middle; }
.tag-row { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 5px; }

.chip { display: inline-block; font-size: 0.76rem; font-weight: 500; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-muted); }

/* --- Filter bar ----------------------------------------------------------- */
.filter-bar { display: flex; gap: 10px; margin-bottom: 18px; flex-wrap: wrap; align-items: center; }
.filter-search { position: relative; flex: 1; min-width: 200px; }
.filter-search input[type="search"] { width: 100%; height: 40px; padding: 0 12px 0 36px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); }
.filter-search .topsearch-icon { left: 12px; }
.filter-bar select { height: 40px; }

/* --- Forms ---------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 0.85rem; font-weight: 600; color: var(--text); }
input[type="text"], input[type="password"], input[type="search"], input[type="date"], input[type="file"], select, textarea {
  width: 100%; font: inherit; font-size: 0.95rem;
  padding: 9px 11px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 60px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.checkbox-field { justify-content: flex-end; }
.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.checkbox input { width: auto; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 8px 0; }
.setting-label { font-weight: 500; }

/* --- Flash + errors ------------------------------------------------------- */
.flash { padding: 11px 14px; border-radius: var(--radius-sm); margin-bottom: 16px; font-size: 0.9rem; }
.flash.err { background: var(--danger-soft); color: var(--danger); }
.flash.ok { background: var(--ok-soft); color: var(--ok); }
.error-detail { white-space: pre-wrap; overflow-x: auto; background: var(--surface-2); padding: 12px; border-radius: var(--radius-sm); font-size: 0.8rem; }

/* --- User list (settings) ------------------------------------------------- */
.user-list { list-style: none; margin: 0 0 8px; padding: 0; }
.user-row { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.user-row:last-child { border-bottom: none; }
.user-row-text { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.user-row-name { font-weight: 600; display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.user-row-sub { font-size: 0.82rem; color: var(--text-muted); }
.user-add-form { margin-top: 6px; }

/* --- Modal ---------------------------------------------------------------- */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; overflow-y: auto; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 12, 18, 0.5); }
.modal-dialog { position: relative; width: 100%; max-width: 560px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); z-index: 1; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; }
.entity-form { padding: 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.entity-form .field { grid-column: auto; }
.entity-form .field-textarea, .entity-form .field-document, .entity-form .field-file, .entity-form .modal-error, .entity-form .modal-actions, .entity-form .field-type, .entity-form .type-fields { grid-column: 1 / -1; }
/* Each type's field set is its own 2-column grid, spanning the form width. */
.type-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.doc-upload { display: flex; align-items: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.doc-upload-label { font-size: 0.85rem; color: var(--text-muted); }
.doc-upload input[type="file"] { width: auto; flex: 1; min-width: 180px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* --- Auth pages ----------------------------------------------------------- */
body.auth { display: flex; flex-direction: column; min-height: 100vh; }
.auth-main { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-md); padding: 30px; }
.auth-card h1 { text-align: center; }
.auth-brand { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 14px; }
.auth-brand .brand-mark { width: 38px; height: 38px; }
.auth-brand .brand-name { font-size: 1.3rem; font-weight: 700; }
.auth-card .page-sub { text-align: center; margin-bottom: 18px; }
.auth-footer { display: flex; justify-content: center; padding: 20px; }

/* --- Empty states --------------------------------------------------------- */
.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); background: var(--surface); border: 1px dashed var(--border-strong); border-radius: var(--radius); }
.empty-state.small { padding: 24px; }
.empty-icon { display: inline-grid; place-items: center; width: 72px; height: 72px; border-radius: 50%; background: var(--surface-2); color: var(--text-muted); margin-bottom: 14px; }
.empty-state p { margin: 0 0 16px; }

/* --- Document detail ------------------------------------------------------ */
.doc-back { margin-bottom: 14px; }
.doc-back a { display: inline-flex; align-items: center; gap: 6px; color: var(--text-muted); font-size: 0.88rem; }
.doc-layout { display: grid; grid-template-columns: minmax(0, 1.8fr) minmax(280px, 1fr); gap: 20px; align-items: start; }
.doc-preview { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); }
.doc-preview iframe { width: 100%; height: 78vh; border: none; display: block; background: #fff; }
.doc-side { display: flex; flex-direction: column; }
.detail-list { margin: 0; display: flex; flex-direction: column; gap: 12px; }
.detail-list div { display: flex; flex-direction: column; gap: 2px; }
.detail-list dt { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); font-weight: 600; }
.detail-list dd { margin: 0; }
.doc-notes { white-space: pre-wrap; margin: 0; }

.comment-list { list-style: none; margin: 0 0 14px; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.comment { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; }
.comment-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.comment-author { font-weight: 600; font-size: 0.88rem; }
.comment-time { font-size: 0.78rem; }
.comment-head .icon-btn { width: 26px; height: 26px; margin-left: auto; }
.comment-body { white-space: pre-wrap; font-size: 0.92rem; }
.comment-form { display: flex; flex-direction: column; gap: 10px; }
.comment-form button { align-self: flex-end; }

/* --- Responsive ----------------------------------------------------------- */
.sidebar-backdrop { position: fixed; inset: 0; background: rgba(10, 12, 18, 0.5); z-index: 35; }

@media (max-width: 900px) {
  .sidebar {
    position: fixed; left: 0; top: 0; height: 100vh;
    transform: translateX(-100%); transition: transform 0.2s ease;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }
  .topbar .menu-toggle { display: inline-grid; }
  .content { padding: 20px 16px 50px; }
  .doc-layout { grid-template-columns: 1fr; }
  .doc-preview iframe { height: 60vh; }
  .entity-form { grid-template-columns: 1fr; }
  .type-fields { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
}

@media (max-width: 560px) {
  .topbar { padding: 0 14px; gap: 10px; }
  .theme-switch { display: none; }
  .head-actions .btn span { display: none; }
}
