:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #1a2330;
  --muted: #5b6878;
  --border: #dde3ea;
  --accent: #1f6feb;
  --accent-dark: #1a5fd0;
  --danger: #c93c37;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 880px;
  margin: 0 auto;
  padding: 0.7rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  font-size: 1.15rem;
  font-weight: 700;
  text-decoration: none;
  color: var(--text);
}
.brand span { color: var(--accent); }

/* --- Account menu --------------------------------------------------------
   A <details> disclosure styled as a dropdown, so it needs no JavaScript to
   open or close. */
.menu { position: relative; }

.menu-trigger {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  user-select: none;
  list-style: none; /* hides the default disclosure triangle */
}
.menu-trigger::-webkit-details-marker { display: none; }
.menu-trigger::marker { content: ''; }
.menu-trigger:hover { background: var(--bg); }
.menu-trigger:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.menu-trigger-name {
  max-width: 11rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Caret drawn in CSS so no icon font or inline SVG is needed. */
.menu-caret {
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid var(--muted);
  transition: transform 0.12s ease;
}
.menu[open] .menu-caret { transform: rotate(180deg); }

.menu-panel {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  z-index: 20;
  min-width: 15rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(16, 28, 44, 0.13);
  overflow: hidden;
}

.menu-meta {
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.menu-meta strong { display: block; font-size: 0.92rem; }
.menu-meta span {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  overflow-wrap: anywhere;
}

.menu-items { display: block; padding: 0.3rem 0; }
.menu-form { padding: 0.3rem 0; border-top: 1px solid var(--border); margin: 0; }

.menu-item {
  display: block;
  width: 100%;
  padding: 0.5rem 0.9rem;
  border: none;
  background: none;
  color: var(--text);
  font-size: 0.92rem;
  font-family: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}
.menu-item:hover { background: var(--bg); }
.menu-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.menu-item-danger { color: var(--danger); }
.menu-item-danger:hover { background: #fdf0ef; }

.container { max-width: 880px; margin: 1.5rem auto; padding: 0 1rem; }

.page-head h1 { margin: 0 0 0.15rem; }
.page-head .sub { margin: 0 0 1rem; color: var(--muted); }
.page-head-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.page-head-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.card-form { max-width: 460px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.card-narrow { max-width: 420px; margin: 3rem auto; }
.card h1 { margin-top: 0; }
.card h2 { margin-top: 0; font-size: 1.05rem; }

label { display: block; margin-bottom: 0.75rem; font-size: 0.92rem; }
input[type="text"], input[type="email"], input[type="password"] {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
input[type="file"] { margin: 0.25rem 0 0.75rem; }

.form-row { max-width: 480px; }

.btn {
  display: inline-block;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.92rem;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: #fdf0ef; }
.btn-small { padding: 0.25rem 0.6rem; font-size: 0.85rem; }

table { width: 100%; border-collapse: collapse; font-size: 0.93rem; }
th {
  text-align: left;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
th, td { padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
td.right, th.right { text-align: right; }
td.actions form.inline { display: inline-block; margin-left: 0.4rem; }

.alert {
  background: #fdf0ef;
  border: 1px solid #f0c4c1;
  color: var(--danger);
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
}
.notice {
  background: #fff8e6;
  border: 1px solid #f0e0b0;
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
}
.hint { color: var(--muted); font-size: 0.85rem; font-weight: normal; }
.empty { color: var(--muted); }

.credential code {
  display: block;
  font-size: 1.3rem;
  padding: 0.8rem;
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: 8px;
  text-align: center;
  letter-spacing: 0.05em;
  user-select: all;
}

.footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
  padding: 1.5rem 0 2rem;
}

/* Long emails and filenames wrap rather than widen the layout, but only when
   they genuinely do not fit — `anywhere` would break every long name mid-word. */
td { overflow-wrap: break-word; }
.container-wide { max-width: 1100px; }

select {
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
  max-width: 100%;
}
.assign { display: flex; gap: 0.4rem; justify-content: flex-end; }

.pill {
  display: inline-block;
  white-space: nowrap;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--muted);
}
.pill-matched, .pill-committed { background: #e8f5ec; border-color: #b7dfc4; color: #1d6b39; }
.pill-unmatched, .pill-pending { background: #fff8e6; border-color: #f0e0b0; color: #8a6100; }
.pill-ambiguous { background: #fdf0ef; border-color: #f0c4c1; color: var(--danger); }

.tally { display: flex; flex-wrap: wrap; gap: 1.25rem; margin-bottom: 0.75rem; }
.tally-item { color: var(--muted); font-size: 0.9rem; }
.tally-item strong { color: var(--text); font-size: 1.35rem; display: block; }
.tally-item.ok strong { color: #1d6b39; }
.tally-item.warn strong { color: #8a6100; }

.row-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.row-actions form.inline { display: inline-block; }

/* --- Folders ---------------------------------------------------------- */
.crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.9rem;
  font-size: 0.95rem;
}
.crumb { color: var(--accent); text-decoration: none; }
.crumb:hover { text-decoration: underline; }
.crumb-current { color: var(--text); font-weight: 600; text-decoration: none; pointer-events: none; }
.crumb-sep { color: var(--muted); }

.folder-table { margin-bottom: 0.75rem; }
.folder-link {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
  margin-right: 0.6rem;
}
.folder-link:hover { color: var(--accent); }

/* Folder glyph drawn in CSS — a tab over a body — so no icon assets. */
.folder-icon {
  position: relative;
  width: 1.05rem;
  height: 0.8rem;
  background: #f7c948;
  border-radius: 2px 3px 3px 3px;
  flex: 0 0 auto;
}
.folder-icon::before {
  content: '';
  position: absolute;
  top: -3px;
  left: 0;
  width: 45%;
  height: 4px;
  background: #f7c948;
  border-radius: 2px 2px 0 0;
}

.move-row td { padding-top: 0; }
.move-row .assign { justify-content: flex-start; align-items: center; }

.search-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.search-bar input[type="search"] {
  flex: 1;
  min-width: 12rem;
  margin-top: 0;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
  font-family: inherit;
}
.search-bar input[type="search"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}
.match-note { display: block; }

.signature-preview {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  background: #fff;
  overflow-x: auto;
}
.signature-preview img { max-width: 100%; height: auto; }

.nowrap { white-space: nowrap; }

/* --- Firm identity ------------------------------------------------------ */
.brand-row { display: flex; flex-wrap: wrap; gap: 1.5rem; align-items: flex-start; }
.brand-preview {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  background: #fff;
  min-width: 12rem;
  text-align: center;
}
.brand-preview img { max-width: 160px; height: auto; display: block; margin: 0 auto 0.5rem; }
.brand-name { margin: 0; font-size: 0.95rem; }
.brand-forms { flex: 1 1 18rem; display: flex; flex-direction: column; gap: 1rem; }
.brand-forms form { margin: 0; }

.check-line { display: flex; align-items: center; gap: 0.5rem; font-size: 0.95rem; margin: 0.5rem 0; }
.check-line input { width: 1rem; height: 1rem; }

.section-head { margin: 2rem 0 0.25rem; }
.section-head + .sub { margin: 0 0 1rem; }

.plain-list { list-style: none; margin: 0; padding: 0; }
.plain-list li {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  line-height: 1.5;
}
.plain-list li:last-child { border-bottom: 0; }

/* --- Bulk selection ----------------------------------------------------- */
.check-col { width: 2.2rem; }
.check-col input { width: 1rem; height: 1rem; }

.bulk-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.6rem 0.75rem;
  margin-bottom: 0.75rem;
  background: #eef4fd;
  border: 1px solid #c9dcf8;
  border-radius: 8px;
}
/* `display: flex` above and the browser's `[hidden] { display: none }` have
   equal specificity, so the component rule would win and the bar would show
   with nothing selected. This restores the hiding explicitly. */
.bulk-bar[hidden] { display: none; }
.bulk-count { font-weight: 600; font-size: 0.9rem; white-space: nowrap; }
.bulk-actions { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.bulk-group { display: flex; align-items: center; gap: 0.3rem; }
.bulk-group input[type="text"] {
  width: 8rem;
  margin-top: 0;
  padding: 0.3rem 0.45rem;
  font-size: 0.88rem;
}

.tag-filter { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 0.9rem; }
.tag-chip {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  text-decoration: none;
}
.tag-chip:hover { border-color: var(--accent); color: var(--accent); }
.tag-chip-on { background: var(--accent); border-color: var(--accent); color: #fff; }
.tag-chip-on .hint { color: #dbe8fb; }
.tag-chip-static { background: var(--bg); font-size: 0.75rem; }
.tag-row { display: inline-flex; gap: 0.25rem; flex-wrap: wrap; margin-left: 0.35rem; }

.file-link { font-weight: 600; color: var(--text); text-decoration: none; }
.file-link:hover { color: var(--accent); text-decoration: underline; }

/* --- Document preview dialog ------------------------------------------- */
.preview-dialog {
  width: min(1000px, 94vw);
  height: min(88vh, 900px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  overflow: hidden;
}
.preview-dialog::backdrop { background: rgba(16, 28, 44, 0.55); }

.preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.preview-title { overflow-wrap: anywhere; }
.preview-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

.preview-body { height: calc(100% - 3.2rem); background: var(--bg); }
.preview-body iframe { width: 100%; height: 100%; border: 0; background: #fff; }

@media (max-width: 640px) {
  .preview-dialog { width: 100vw; height: 100vh; max-width: none; border-radius: 0; border: 0; }
}

.storage-verdict { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.storage-ok { color: #1d6b39; font-weight: 600; }
.storage-bad { color: var(--danger); font-weight: 600; }

pre.sample {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.75rem;
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.5;
}

.form-inline { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.form-inline input[type="text"] { flex: 1; min-width: 12rem; width: auto; margin-top: 0; }

textarea {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
  font-family: inherit;
  resize: vertical;
}
textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }

label.check {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  font-size: 0.95rem;
}
label.check input { flex: 0 0 auto; }

/* --- File browser toolbar ---------------------------------------------- */
.browser-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}
.browser-bar .crumbs { margin-bottom: 0.4rem; }
.browser-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

.tool { position: relative; }
.tool > summary { list-style: none; user-select: none; }
.tool > summary::-webkit-details-marker { display: none; }
.tool > summary::marker { content: ''; }
.tool[open] > summary { background: var(--bg); }

.tool-panel {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  z-index: 15;
  width: 19rem;
  max-width: 88vw;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(16, 28, 44, 0.13);
  padding: 0.9rem;
}
.tool-panel label { font-size: 0.88rem; }
.tool-panel form.dropzone-active { outline: 2px dashed var(--accent); outline-offset: 4px; }

.drop-hint { margin: 0.35rem 0 0.75rem; }
.drop-feedback { margin: 0 0 0.75rem; font-weight: 600; color: #1d6b39; }
.drop-feedback-error { color: var(--danger); }

.folder-row.drop-target-active td { background: #eef4fd; }
.folder-row.drop-target-active .folder-link { color: var(--accent); }
.card.drop-target-active { border-color: var(--accent); }

/* --- Drag-and-drop upload --------------------------------------------- */
.dropzone-surface {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 0.75rem;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}
.dropzone-surface.dropzone-active {
  border-color: var(--accent);
  background: #eef4fd;
}
.dropzone-hint { margin: 0 0 0.5rem; color: var(--muted); font-size: 0.9rem; }
.dropzone-status { margin: 0 0 0.5rem; font-size: 0.9rem; color: #1d6b39; font-weight: 600; }
.dropzone-surface input[type="file"] { margin: 0; }

/* --- Phones ---
   A five-column table cannot fit a phone viewport; left as a table it forces
   the layout viewport wider than the device, which makes the whole page render
   zoomed out. Below this width each row becomes a stacked card with its column
   name as an inline label. */
@media (max-width: 640px) {
  .container { margin: 1rem auto; }
  .card { padding: 1rem; }

  /* Popover panels anchor to the screen edge rather than the trigger so they
     can never hang off the side of a narrow screen. */
  .tool { position: static; }
  .tool-panel {
    position: fixed;
    left: 0.75rem;
    right: 0.75rem;
    top: auto;
    width: auto;
    max-width: none;
  }
  .browser-bar { flex-direction: column; }
  .browser-actions { align-self: stretch; }
  .browser-actions .btn { flex: 1; text-align: center; }

  .menu-trigger-name { max-width: 7rem; }
  .menu-panel {
    position: fixed;
    top: 3.2rem;
    left: 0.75rem;
    right: 0.75rem;
    min-width: 0;
  }
  .menu-item { padding: 0.7rem 0.9rem; }

  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }

  tr {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.6rem;
  }
  tbody tr:last-child { margin-bottom: 0; }

  td, tbody tr:last-child td { border-bottom: none; padding: 0.3rem 0; }

  td[data-label] {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 1rem;
  }
  td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 auto;
    color: var(--muted);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }
  td.right, th.right { text-align: right; }

  /* Row actions become comfortably tappable buttons sharing the width evenly.
     Each direct child gets an equal flex basis, otherwise a full-width anchor
     starves the sibling form and its label wraps one letter per line. */
  td.actions { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
  td.actions > * { flex: 1 1 0; min-width: 0; margin-left: 0; }
  td.actions form.inline { display: block; }
  td.actions .btn {
    display: block;
    width: 100%;
    text-align: center;
    white-space: nowrap;
  }
  .btn-small { padding: 0.5rem 0.6rem; font-size: 0.9rem; }

  /* Redundant on a phone: the row's title is already a link. */
  .hide-sm { display: none; }
}
