/* ==========================================================================
   Documents — the shared library on a firm record, a case, a review and a task.

   One component for all three of its views: the flat list (which reuses the
   app's table styles), the folder explorer and the tree. What is common to them
   — the toolbar, the file and version pills, the empty states — sits at the top.
   ========================================================================== */
.documents {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  width: 100%;
  min-width: 0;
}

/* == "storage is not connected" banner ==
   Sits above everything, once, for as long as there is no bucket behind the
   library (see Document.storage_connected?). */
.documents__notice {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-warning);
  border-radius: var(--radius-md);
  background-color: var(--color-surface-sunken);
  color: var(--color-text-muted);
  font-size: var(--font-size-small);
  line-height: 1.5;
}

.documents__notice-icon {
  flex: 0 0 auto;
  margin-top: 1px;
  color: var(--color-warning);
}

/* == toolbar == */
.documents__toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.documents__controls {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

/* == view switcher ==
   A segmented control: three links that look like one control, so which view is
   open reads as a position rather than as a highlighted link. */
.documents__views {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background-color: var(--color-surface-sunken);
}

.documents__view {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-size: var(--font-size-small);
  font-family: var(--font-family-bold);
  font-weight: 600;
  text-decoration: none;
  transition: background-color var(--transition-base), color var(--transition-base);
}

.documents__view:hover {
  color: var(--color-primary-dark);
}

.documents__view--active {
  background-color: var(--color-surface);
  color: var(--color-primary-dark);
  box-shadow: 0 1px 2px var(--tint-navy-10);
}

/* == list view == */
.documents__name-cell {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.documents__file-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background-color: var(--tint-navy-06);
  color: var(--color-primary-dark);
}

.documents__name {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.documents__name-title {
  font-weight: 600;
  color: var(--color-text);
}

/* The filename under the document's name: the same file can be filed under a
   name nobody would recognise it by, so both are shown. */
.documents__name-file {
  font-size: var(--font-size-small);
  color: var(--color-text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* == version pill ==
   Tinted once a document has history behind it, so a replaced file is obvious
   from the row without opening it. */
.documents__version-pill {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
  background-color: var(--tint-navy-06);
  color: var(--color-text-muted);
  font-size: var(--font-size-small);
  font-family: var(--font-family-bold);
  font-weight: 600;
  white-space: nowrap;
}

.documents__version-pill--history {
  background-color: var(--tint-green-16);
  color: var(--color-primary-dark);
}

.documents__version-active {
  padding: 1px var(--space-2);
  border-radius: var(--radius-full);
  background-color: var(--color-primary-green);
  color: var(--color-white);
  font-size: var(--font-size-small);
  font-weight: 600;
  white-space: nowrap;
}

/* == "showing one folder only" strip, above the list == */
.documents__scope {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  background-color: var(--tint-navy-04);
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
}

.documents__scope-icon {
  color: var(--color-primary-dark);
}

.documents__scope-clear {
  margin-left: auto;
  color: var(--color-primary-dark);
  font-weight: 600;
  text-decoration: none;
}

.documents__scope-clear:hover {
  text-decoration: underline;
}

/* == folder view == */
.documents__folder-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: var(--space-3);
  list-style: none;
  margin: 0;
  padding: 0;
}

.documents__folder {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  height: 100%;
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background-color: var(--color-surface);
  text-decoration: none;
  transition: border-color var(--transition-base), box-shadow var(--transition-base),
    transform var(--transition-base);
}

.documents__folder:hover {
  border-color: var(--color-primary-green);
  box-shadow: 0 2px 8px var(--tint-navy-10);
  transform: translateY(-1px);
}

/* An empty folder is still a folder — it is part of the filing plan — but it
   should not compete with the ones holding something. */
.documents__folder--empty {
  background-color: var(--color-surface-sunken);
}

.documents__folder--empty .documents__folder-icon,
.documents__folder--empty .documents__folder-name {
  color: var(--color-text-faint);
}

.documents__folder-icon {
  color: var(--color-primary-green);
}

.documents__folder-name {
  font-family: var(--font-family-bold);
  font-weight: 600;
  color: var(--color-primary-dark);
}

.documents__folder-meta {
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
}

/* == open folder == */
.documents__folder-path {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding-bottom: var(--space-3);
  border-bottom: 1px solid var(--color-border);
  font-size: var(--font-size-medium);
}

.documents__folder-up {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-muted);
  text-decoration: none;
}

.documents__folder-up:hover {
  color: var(--color-primary-dark);
}

.documents__folder-sep {
  color: var(--color-text-faint);
}

.documents__folder-current {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-family-bold);
  font-weight: 600;
  color: var(--color-primary-dark);
}

.documents__folder-count {
  margin-left: auto;
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
}

.documents__files {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  list-style: none;
  margin: 0;
  padding: 0;
}

.documents__file {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background-color: var(--color-surface);
}

.documents__file-detail {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0;
}

.documents__file-name {
  font-weight: 600;
  color: var(--color-text);
}

.documents__file-meta {
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* == tree view ==
   Built from <details>, so the browser owns expand/collapse. The caret is the
   only thing that has to know which way round it is. */
.documents__tree {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background-color: var(--color-surface);
}

.documents__node {
  border-radius: var(--radius-sm);
}

.documents__node--document,
.documents__node-empty,
.documents__node > p {
  /* Indent one level per step down the tree, with a guide line back up to the
     parent so a deep list stays readable. */
  margin-left: var(--space-4);
  padding-left: var(--space-3);
  border-left: 1px solid var(--color-border);
}

.documents__node-summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  list-style: none;
}

.documents__node-summary::-webkit-details-marker {
  display: none;
}

.documents__node-summary:hover {
  background-color: var(--tint-navy-04);
}

.documents__node-caret {
  display: inline-flex;
  flex: 0 0 auto;
  color: var(--color-text-faint);
  transition: transform var(--transition-base);
}

.documents__node[open] > .documents__node-summary .documents__node-caret {
  transform: rotate(90deg);
}

.documents__node-icon {
  display: inline-flex;
  flex: 0 0 auto;
  color: var(--color-primary-green);
}

.documents__node--document > .documents__node-summary .documents__node-icon {
  color: var(--color-primary-dark);
}

.documents__node-label {
  font-weight: 600;
  color: var(--color-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.documents__node-meta {
  font-size: var(--font-size-small);
  color: var(--color-text-faint);
}

.documents__node-count {
  margin-left: auto;
  padding: 1px var(--space-2);
  border-radius: var(--radius-full);
  background-color: var(--tint-navy-06);
  color: var(--color-text-muted);
  font-size: var(--font-size-small);
  font-weight: 600;
}

/* The version pill and count both want the right-hand end; whichever comes
   first takes it and the rest follow. */
.documents__node-summary .documents__version-pill {
  margin-left: auto;
}

.documents__node-summary .documents__node-count ~ .documents__version-pill {
  margin-left: 0;
}

.documents__node-actions {
  margin-left: var(--space-4);
  padding: 0 var(--space-3) var(--space-2);
}

.documents__node-empty {
  padding: var(--space-2) 0;
  font-size: var(--font-size-small);
  color: var(--color-text-faint);
}

.documents__versions {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin: 0 0 var(--space-2) var(--space-4);
  padding-left: var(--space-3);
  border-left: 1px solid var(--color-border);
  list-style: none;
}

.documents__version {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
}

.documents__version--current {
  background-color: var(--tint-green-10);
}

.documents__version-label {
  font-family: var(--font-family-bold);
  font-weight: 600;
  color: var(--color-primary-dark);
}

.documents__version-file {
  color: var(--color-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.documents__version-meta {
  margin-left: auto;
  color: var(--color-text-faint);
  white-space: nowrap;
}

/* == empty states == */
.documents__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-6) var(--space-4);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-md);
  background-color: var(--color-surface);
  text-align: center;
  color: var(--color-text-muted);
}

.documents__empty--first {
  padding: var(--space-8) var(--space-4);
}

.documents__empty-icon {
  color: var(--color-text-faint);
}

.documents__empty-title {
  margin: 0;
  font-size: var(--font-size-large);
}

.documents__empty p {
  max-width: 52ch;
  line-height: 1.6;
}

/* == upload control ==
   The real input is laid transparently over the whole zone rather than hidden
   away from it: the label below is the styling, the input is the hit area, and
   nothing has to forward a click. Transparent rather than clipped or display:
   none, so it keeps its size and stays reachable — a clipped input is one
   nothing driving the page can be made to see. */
.documents__dropzone {
  position: relative;
}

.documents__dropzone-input {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.documents__dropzone-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-5) var(--space-4);
  border: 1px dashed var(--color-border-strong);
  border-radius: var(--radius-md);
  background-color: var(--color-surface-sunken);
  color: var(--color-text-muted);
  text-align: center;
  cursor: pointer;
  transition: border-color var(--transition-base), background-color var(--transition-base);
}

.documents__dropzone-label:hover {
  border-color: var(--color-primary-green);
}

/* Keyboard users focus the input, which is off screen — so the label wearing
   the focus ring is the only way the focus is visible. */
.documents__dropzone-input:focus-visible + .documents__dropzone-label {
  border-color: var(--color-primary-green);
  outline: 2px solid var(--color-primary-green);
  outline-offset: 2px;
}

.documents__dropzone--filled .documents__dropzone-label {
  border-style: solid;
  border-color: var(--color-primary-green);
  background-color: var(--tint-green-10);
  color: var(--color-primary-dark);
}

.documents__dropzone-prompt {
  font-family: var(--font-family-bold);
  font-weight: 600;
  overflow-wrap: anywhere;
}

.documents__dropzone-hint {
  font-size: var(--font-size-small);
  color: var(--color-text-faint);
}

.documents__dropzone--filled .documents__dropzone-hint {
  display: none;
}

/* == drawer: document detail and version history == */
.documents__drawer-summary {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3);
  margin-bottom: var(--space-4);
  border-radius: var(--radius-md);
  background-color: var(--color-surface-sunken);
}

.documents__drawer-name {
  margin: 0;
  font-family: var(--font-family-bold);
  font-weight: 600;
  color: var(--color-primary-dark);
}

.documents__drawer-meta {
  margin: 0;
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
}

.documents__detail {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.documents__detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3) var(--space-4);
  margin: 0;
}

.documents__detail-grid dt {
  font-size: var(--font-size-small);
  text-transform: uppercase;
  letter-spacing: var(--label-letter-spacing);
  color: var(--color-text-muted);
}

.documents__detail-grid dd {
  margin: var(--space-1) 0 0;
  color: var(--color-text);
  overflow-wrap: anywhere;
}

.documents__detail-description {
  padding: var(--space-3);
  border-radius: var(--radius-md);
  background-color: var(--color-surface-sunken);
  color: var(--color-text-muted);
  line-height: 1.6;
}

.documents__detail-heading {
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-border);
  font-size: var(--font-size-medium);
  text-transform: uppercase;
  letter-spacing: var(--label-letter-spacing);
  color: var(--color-text-muted);
}

.documents__history {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  margin: 0;
  padding: 0;
  list-style: none;
}

.documents__history-item {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.documents__history-item--current {
  border-color: var(--color-primary-green);
  background-color: var(--tint-green-10);
}

.documents__history-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.documents__history-file {
  font-weight: 600;
  color: var(--color-text);
  overflow-wrap: anywhere;
}

.documents__history-meta {
  font-size: var(--font-size-small);
  color: var(--color-text-muted);
}

.documents__history-notes {
  padding: var(--space-2);
  border-radius: var(--radius-sm);
  background-color: var(--color-surface-sunken);
  font-size: var(--font-size-small);
  color: var(--color-text);
}

.documents__history-actions {
  display: flex;
  gap: var(--space-2);
}

/* == narrow screens ==
   The library appears inside a tab and inside the firm record's content column,
   both of which are already narrow before the viewport is. */
@media (max-width: 768px) {
  .documents__toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .documents__controls {
    flex-direction: column;
    align-items: stretch;
  }

  .documents__detail-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .documents__file {
    flex-wrap: wrap;
  }
}

/* Client documents — the Source column, which names the case, review or report a
   row came from and links to it. The pill and the link sit on one line so the
   column reads as one thing rather than two. */
.documents__source {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

/* A row filed before the bucket was connected: the record is real and the file
   was never kept, so the action cell says so rather than offering a download
   that cannot work (see DocumentStorage). */
.documents__unstored {
  color: var(--color-text-muted);
  font-size: 0.8125rem;
  font-style: italic;
}
