/*
 * Exactly fills the visible viewport, so the page itself never scrolls; only the library and the module panes do.
 * The toolbar takes its natural height and the workspace gets the rest, rather than computing it.
 */
accordance-workspace {
  display: flex;
  flex-direction: column;
  font-size: 1rem;
  height: 100vh;
  height: 100dvh;
  width: 100%;
  overflow: hidden;

  --workspace-grid-gap-size: 3px;

  --toolbar-icon-size: 22px;
}

accordance-toolbar {
  display: block;
  flex: none;
}

/*
 * Flex rather than a grid, so the documents always take the rest of the width: with the library hidden, a grid
 * auto-places them into the library's content-sized column instead. The library's divider is the gap beside it.
 */
#workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  background-color: var(--tint-strong);
  /* Keeps the panes' own stacking (like the library's sticky search) beneath the toolbar's mobile menu */
  isolation: isolate;
}

#main {
  flex: 1;
  min-width: 0;
}

#toolbar {
  background-color: var(--surface);
  user-select: none;
  -webkit-user-select: none;
}

#toolbar-container {
  height: 64px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: solid;
  border-width: 0 0 var(--workspace-grid-gap-size) 0;
  border-color: var(--tint-strong);
  user-select: none;
  -webkit-user-select: none;
}

#instant-details {
  display: none;
}

/* Icons are centered by their rows, so they only need horizontal spacing */
#toolbar {
  align-items: center;
}

/* The toolbar's buttons: an icon (components/icons.js) in the text colour, on a tile that shows hover and "on" */
.tool {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px;
  margin: 0 2px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition:
    background-color 0.15s ease,
    color 0.15s ease;
}

.tool .icon {
  display: block;
  width: var(--toolbar-icon-size);
  height: var(--toolbar-icon-size);
}

.tool:hover {
  background-color: var(--surface-hover);
}

.tool:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 1px;
}

/* Not built yet; still hoverable, for its "coming soon" tooltip */
.tool.coming-soon,
.tool.coming-soon:hover {
  opacity: 0.3;
  background: none;
  cursor: not-allowed;
}

#toolbar > .tool:first-child {
  margin-left: 8px;
}

#toolbar-admin-section {
  user-select: none;
  -webkit-user-select: none;
  display: flex;
  align-items: center;
}

#toolbar-admin-section > :last-child {
  margin-right: 5px;
}

#toolbar-admin-section font-size-adjuster {
  --font-size-icon-size: var(--toolbar-icon-size);
}

/* Shown at every width, between the tools (or the menu button) and the account section */
#toolbar-container .beta-badge {
  flex: none;
  padding: 1px 5px;
  border: 1.5px solid currentColor;
  border-radius: 4px;
  color: var(--beta-badge);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.2;
}

/* The documents, then instant details under them, with the divider between as the gap */
#main {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto auto;
}

/* The documents, and a rail of those hidden for lack of room */
#documentArea {
  position: relative;
  display: flex;
  min-width: 0;
  min-height: 0;
}

#documentContainer {
  flex: 1;
  min-width: 0;
  display: flex;
  overflow: hidden;
  justify-content: space-between;
}

#hidden-documents {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 4px;
  overflow: hidden;
  background: var(--surface);
  border-left: 1px solid var(--divider);
}

#hidden-documents button {
  writing-mode: vertical-rl;
  max-height: 12em;
  padding: 0.75em 0.3em;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 0.5em;
  background-color: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  white-space: nowrap;
  text-overflow: ellipsis;
  /* Matches the documents' abbreviations, which drag the same way */
  cursor: grab;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease;
}

#hidden-documents button:hover,
#hidden-documents button:focus-visible {
  border-color: var(--border-hover);
  background-color: var(--surface-hover);
}

#hidden-documents button {
  /* Dragged rather than scrolled or zoomed by a finger */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* Just opened here for lack of room, so it's noticed */
#hidden-documents button.flash {
  animation: hidden-document-flash 0.4s ease-in-out 6 alternate;
}

@keyframes hidden-document-flash {
  to {
    border-color: var(--accent);
    background-color: var(--accent-bg);
    box-shadow: 0 0 0 2px var(--accent-glow);
  }
}

/* Being dragged to a new place */
text-doc.dragging,
tool-doc.dragging,
#hidden-documents button.dragging {
  opacity: 0.4;
}

body.dragging-document,
body.dragging-document * {
  cursor: grabbing !important;
  user-select: none;
  -webkit-user-select: none;
}

#document-drag-ghost {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  margin: 8px 0 0 8px;
  padding: 0.25em 0.6em;
  border: 1px solid var(--border-hover);
  border-radius: 0.5em;
  background: var(--surface);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  font-size: 0.85rem;
  white-space: nowrap;
  pointer-events: none;
}

#document-drop-indicator {
  position: fixed;
  z-index: 999;
  border-radius: 2px;
  background: var(--accent);
  pointer-events: none;
}

/* Over the empty document area when every document is in the rail, or none is open */
#no-documents {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 1em;
  color: var(--text-muted);
  text-align: center;
  pointer-events: none;
}

/* Shown while a document is dragged and the rail is empty, over the documents' edge so they don't shift */
#hidden-documents.drop-only {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 998;
  justify-content: center;
  align-items: center;
  writing-mode: vertical-rl;
  width: 2.5em;
  padding: 0;
  border: 2px dashed var(--border);
  background: var(--surface-raised);
  color: var(--text-muted);
  font-size: 0.8rem;
  white-space: nowrap;
  opacity: 0.95;
}

#hidden-documents.drop-only.drop-over {
  border-color: var(--accent);
  background: var(--accent-bg);
  color: var(--text);
}

/* Linked, as on the documents' own link toggle */
#hidden-documents button.linked {
  border-color: var(--linked-border);
  background-color: var(--linked-bg);
  color: var(--linked-text);
}

/* Phones stack their documents, with the rail underneath */
@media (max-width: 479px) {
  #documentArea,
  #documentContainer {
    flex-direction: column;
  }

  #documentContainer > * {
    flex: 1 1 0;
    min-height: 0;
  }

  #hidden-documents {
    flex-direction: row;
    padding: 4px 8px;
    border-left: none;
    border-top: 1px solid var(--divider);
  }

  #hidden-documents.drop-only {
    top: auto;
    left: 0;
    width: auto;
    height: 2.5em;
    padding: 0;
    writing-mode: horizontal-tb;
    border: 2px dashed var(--border);
  }

  #hidden-documents button {
    writing-mode: horizontal-tb;
    max-width: 12em;
    max-height: none;
    padding: 0.3em 0.75em;
  }
}

#empty-text {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

text-doc,
tool-doc {
  width: 100%;
}

/* Each document's share of the row, set by the workspace (equal shares are each 1). Phones stack them evenly instead. */
text-doc,
tool-doc {
  flex: var(--document-weight, 1) 1 0;
}

/* One document maximized takes the row; the others keep to the narrowest a divider can make them
   (MIN_RESIZED_DOCUMENT_WIDTH) */
@media (min-width: 480px) {
  #documentContainer.has-maximized > :is(text-doc, tool-doc) {
    flex: 0 0 300px;
  }

  #documentContainer.has-maximized > [maximized] {
    flex: 1 1 0;
  }
}

/*
 * Dragged to resize the panes either side. Drawn as the 3px gap between panes, with a wider grip than it looks. Between
 * documents, the workspace places each one with `order`, between the two it divides.
 */
.pane-divider {
  position: relative;
  z-index: 1;
  flex: none;
  background-color: var(--tint-strong);
  touch-action: none;
  outline: none;
  transition: background-color 0.15s ease;
}

.pane-divider::before {
  content: "";
  position: absolute;
}

.pane-divider[aria-orientation="vertical"] {
  width: var(--workspace-grid-gap-size);
  cursor: col-resize;
}

.pane-divider[aria-orientation="vertical"]::before {
  inset: 0 -3px;
}

.pane-divider[aria-orientation="horizontal"] {
  height: var(--workspace-grid-gap-size);
  cursor: row-resize;
}

.pane-divider[aria-orientation="horizontal"]::before {
  inset: -3px 0;
}

/* Shown once the pointer rests on it, so passing over doesn't flash it */
.pane-divider:hover {
  background-color: var(--accent);
  transition-delay: 0.3s;
}

.pane-divider:focus-visible,
.pane-divider.resizing {
  background-color: var(--accent);
  transition-delay: 0s;
}

/* Gone with the pane it resizes */
library-elem[style*="display: none"] + .pane-divider,
#main:has(> instant-details[style*="display: none"]) > .pane-divider {
  display: none;
}

body.resizing-panes,
body.resizing-panes * {
  user-select: none;
  -webkit-user-select: none;
}

body.resizing-panes[data-resize="vertical"] * {
  cursor: col-resize !important;
}

body.resizing-panes[data-resize="horizontal"] * {
  cursor: row-resize !important;
}

@media (min-width: 480px) {
  text-doc,
  tool-doc {
    min-width: 240px;
  }
}

/* Phones and narrow windows size their panes themselves */
@media (max-width: 479px) {
  .pane-divider {
    display: none;
  }
}

text-doc,
tool-doc,
library-elem,
instant-details {
  background: var(--surface);
}

/* Wide at the sides: it sits at the bottom of the screen, where rounded corners on phones and tablets cut in */
instant-details {
  padding: 0.75em 1.25em;
}

@media (min-width: 480px) {
  accordance-workspace {
    --toolbar-icon-size: 26px;
  }

  #instant-details {
    display: inline;
  }
}

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: var(--surface);
  padding: 15px 20px;
  position: relative;
}

.menu-toggle {
  display: none;
}

.nav-links {
  display: flex;
  list-style: none;
  gap: 20px;
}

.hamburger {
  display: none;
  cursor: pointer;
  flex-direction: column;
  gap: 5px;
}

.bar {
  width: 25px;
  height: 3px;
  background-color: var(--text);
  transition: 0.3s ease;
}

/* TODO: reverse this to min-width */
@media (max-width: 768px) {
  .hamburger {
    display: flex;
  }

  .nav-links {
    display: none;
    width: 100%;
    /* The theme's own menu colour, so the icons (black in light, inverted in dark) read on it */
    background-color: var(--surface-raised);
    border-bottom: 1px solid var(--divider);
    position: absolute;
    top: 100%; /* Sits directly beneath navbar */
    left: 0;
    padding: 20px;
    gap: 15px;
    box-shadow: 0px 4px 3px 3px hsla(0, 0%, 10%, 10%);
    justify-content: space-evenly;
    z-index: 1;
  }

  .menu-toggle:checked ~ .nav-links {
    display: flex;
  }

  .menu-toggle:checked ~ .hamburger .bar:nth-child(1) {
    transform: translateY(8px) rotate(45deg);
  }
  .menu-toggle:checked ~ .hamburger .bar:nth-child(2) {
    opacity: 0;
  }
  .menu-toggle:checked ~ .hamburger .bar:nth-child(3) {
    transform: translateY(-8px) rotate(-45deg);
  }
}
