:root {
  color-scheme: light;

  --bg: #eaecef;
  --panel: #ffffff;
  --surface: #f8f9fa;
  --sunken: #f1f2f4;
  --border: #d8dadd;
  --rule: #e4e6e9;
  --text: #2f343b;
  --muted: #6f7782;
  --muted-2: #a8b0ba;
  --accent: #008818;
  --accent-hover: #007214;
  --accent-soft: #eef7f0;
  --danger: #b42318;
  --danger-soft: #fff4f2;

  --radius: 4px;
  --gutter: 42px;
  --indent: 40px;
  --row-h: 58px;
  --panel-w: 440px;
  --control-h: 34px;
  --content-pad: 12px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  overscroll-behavior: none;
}

button,
input,
textarea {
  font: inherit;
}

button,
.row__line {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
.row__line:focus-visible {
  outline: 2px solid rgba(0, 136, 24, 0.28);
  outline-offset: 2px;
}

[hidden],
.hidden {
  display: none !important;
}

/* ---------- Shell ---------- */

.app {
  display: grid;
  grid-template-columns: var(--content-pad) minmax(0, 1fr) auto var(--content-pad);
  grid-template-rows: auto var(--content-pad) minmax(0, 1fr) var(--content-pad);
  height: 100%;
  height: 100dvh;
}

/* ---------- Top bar ---------- */

.topbar {
  align-items: center;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  grid-column: 1 / -1;
  grid-row: 1;
  min-height: 54px;
  padding: 9px 16px;
  padding-top: calc(9px + env(safe-area-inset-top, 0px));
  z-index: 3;
}

.topbar__identity {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 10px;
  min-width: min(100%, 220px);
}

.topbar__title {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0;
  line-height: 1.2;
  margin: 0;
}

.topbar__meta {
  align-items: center;
  display: flex;
  flex: 1;
  gap: 10px;
  justify-content: flex-end;
  min-width: 120px;
}

.status {
  color: var(--muted);
  font-size: 13px;
}

.status--error {
  color: var(--danger);
  font-weight: 600;
}

/* ---------- Buttons ---------- */

.btn {
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  font-weight: 500;
  justify-content: center;
  min-height: var(--control-h);
  padding: 6px 12px;
  touch-action: manipulation;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: var(--surface);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  cursor: default;
  opacity: 0.48;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.btn--primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn--danger {
  background: var(--panel);
  border-color: #f0b8b3;
  color: var(--danger);
  flex: 0 0 auto;
  margin: 0 16px 16px;
  width: calc(100% - 32px);
}

.btn--danger:hover:not(:disabled) {
  background: var(--danger-soft);
  border-color: #ea8f88;
}

.icon-btn {
  align-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  flex: none;
  font-size: 24px;
  height: 34px;
  justify-content: center;
  line-height: 1;
  padding: 0;
  width: 34px;
}

.icon-btn:hover {
  background: var(--sunken);
  color: var(--text);
}

/* ---------- Confirm dialog ---------- */

.confirm {
  background: transparent;
  border: 0;
  color: var(--text);
  margin: auto;
  max-width: calc(100vw - 32px);
  padding: 0;
  width: 430px;
}

.confirm::backdrop {
  background: rgba(47, 52, 59, 0.46);
}

.confirm__box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 22px 58px rgba(47, 52, 59, 0.26);
  overflow: hidden;
}

.confirm__head {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
}

.confirm__title {
  color: var(--text);
  font-size: 18px;
  font-weight: 600;
  line-height: 1.25;
  margin: 0;
}

.confirm__message {
  color: var(--muted);
  margin: 0;
  padding: 16px;
}

.confirm__actions {
  align-items: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 10px 16px;
}

.confirm__danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #ffffff;
}

.confirm__danger:hover:not(:disabled) {
  background: #961c14;
  border-color: #961c14;
}

/* ---------- Sheet ---------- */

.sheet {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  grid-column: 2;
  grid-row: 3;
  overflow: auto;
  overscroll-behavior: contain;
  position: relative;
}

.app:has(.panel:not([hidden])) .sheet {
  border-bottom-right-radius: 0;
  border-top-right-radius: 0;
}

.rows {
  padding-bottom: 30vh;
  position: relative;
}

.root-add {
  bottom: calc(var(--content-pad) + 14px + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 8px 22px rgba(47, 52, 59, 0.14);
  left: calc(var(--content-pad) + 14px + env(safe-area-inset-left, 0px));
  position: fixed;
  z-index: 4;
}

/* ---------- Rows ---------- */

.row {
  align-items: stretch;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  cursor: default;
  display: grid;
  grid-template-columns: var(--gutter) minmax(0, 1fr);
  min-height: var(--row-h);
  position: relative;
}

.row:hover,
.row__line:focus-visible {
  background: var(--surface);
}

.row.is-selected {
  background: #f5f6f7;
}

.row.is-selected::before,
.row.is-drop-into::before {
  bottom: 0;
  content: '';
  left: 0;
  position: absolute;
  top: 0;
  width: 3px;
}

.row.is-selected::before,
.row.is-drop-into::before {
  background: var(--accent);
}

.row--draft,
.row.is-drop-into {
  background: var(--accent-soft);
}

.row.is-dragging {
  opacity: 0.45;
}

.row__grip {
  align-items: center;
  color: var(--muted-2);
  cursor: grab;
  display: flex;
  justify-content: center;
  touch-action: none;
}

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

.row__grip:active {
  cursor: grabbing;
}

.row__grip-mark {
  height: 22px;
  pointer-events: none;
  width: 22px;
}

.row__line {
  align-items: center;
  cursor: pointer;
  display: flex;
  gap: 10px;
  min-height: var(--row-h);
  min-width: 0;
  padding: 6px 16px 6px calc(10px + var(--depth, 0) * var(--indent));
}

.row__twist {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: var(--radius);
  color: var(--accent);
  cursor: pointer;
  display: flex;
  flex: none;
  height: 34px;
  justify-content: center;
  padding: 0;
  width: 28px;
}

.row__twist:hover {
  background: var(--sunken);
}

.row__twist--empty {
  pointer-events: none;
  visibility: hidden;
}

.row__twist-mark {
  height: 16px;
  pointer-events: none;
  transition: transform 0.12s ease;
  width: 16px;
}

.row__twist[aria-expanded='true'] .row__twist-mark {
  transform: rotate(90deg);
}

.row__folder {
  align-items: center;
  color: var(--accent);
  display: inline-flex;
  flex: none;
  height: 34px;
  justify-content: center;
  width: 28px;
}

.row__folder-mark {
  height: 24px;
  pointer-events: none;
  width: 24px;
}

.row__folder--draft {
  opacity: 0.78;
}

.row__content {
  align-items: center;
  display: flex;
  flex: 1;
  gap: 10px;
  min-width: 0;
}

.row__name {
  color: var(--accent);
  display: block;
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 0;
  line-height: 1.25;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row__count {
  color: var(--muted);
  flex: none;
  font-size: 17px;
  line-height: 1;
}

.row__notes {
  align-items: center;
  color: var(--muted);
  display: inline-flex;
  flex: none;
  font-size: 13px;
  gap: 4px;
  line-height: 1;
}

.row__notes-mark {
  height: 15px;
  opacity: 0.85;
  width: 15px;
}

.row__input {
  background: var(--panel);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  color: var(--text);
  flex: 1;
  font-size: 17px;
  font-weight: 500;
  min-height: 36px;
  min-width: 120px;
  padding: 6px 10px;
}

.row__input:focus-visible {
  outline: 2px solid rgba(0, 136, 24, 0.22);
}

.row__add {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: var(--radius);
  color: var(--accent);
  cursor: pointer;
  display: inline-flex;
  flex: none;
  height: 30px;
  justify-content: center;
  padding: 0;
  width: 30px;
}

.row__add:hover {
  background: var(--sunken);
  color: var(--accent-hover);
}

.row__add-mark {
  height: 18px;
  pointer-events: none;
  width: 18px;
}

/* ---------- Dragging ---------- */

.drop-line {
  background: var(--accent);
  border-radius: 999px;
  height: 3px;
  left: calc(var(--gutter) + 10px + var(--depth, 0) * var(--indent));
  pointer-events: none;
  position: absolute;
  right: 16px;
  transform: translateY(-2px);
  z-index: 4;
}

.ghost {
  background: var(--text);
  border-radius: var(--radius);
  color: #ffffff;
  font-size: 13px;
  font-weight: 600;
  left: 0;
  max-width: 320px;
  overflow: hidden;
  padding: 7px 10px;
  pointer-events: none;
  position: fixed;
  text-overflow: ellipsis;
  top: 0;
  white-space: nowrap;
  z-index: 20;
}

body.is-dragging-row {
  cursor: grabbing;
  user-select: none;
}

body.is-dragging-row .row:hover {
  background: var(--panel);
}

/* ---------- Empty state ---------- */

.empty {
  color: var(--muted);
  margin: 56px auto;
  max-width: 520px;
  padding: 30px 24px;
  text-align: center;
}

.empty h2 {
  color: var(--text);
  font-size: 22px;
  font-weight: 600;
  line-height: 1.2;
  margin: 0 0 8px;
}

.empty p {
  font-size: 15px;
  margin: 0 0 20px;
}

/* ---------- Panel ---------- */

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 0;
  border-radius: 0 var(--radius) var(--radius) 0;
  display: flex;
  flex-direction: column;
  gap: 0;
  grid-column: 3;
  grid-row: 3;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  width: var(--panel-w);
}

.panel__head {
  align-items: flex-start;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
  padding: 14px 16px;
  position: sticky;
  top: 0;
  z-index: 3;
}

.panel__name {
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text);
  flex: 1;
  font-size: 21px;
  font-weight: 600;
  line-height: 1.2;
  min-height: 38px;
  min-width: 0;
  padding: 5px 7px;
}

.panel__name:hover {
  border-color: var(--border);
}

.panel__name:focus {
  background: var(--panel);
  border-color: var(--accent);
  outline: none;
}

.panel__moves {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: grid;
  flex: 0 0 auto;
  gap: 8px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  padding: 10px 16px;
}

.panel__move {
  min-height: 34px;
  min-width: 0;
}

/* ---------- Composer ---------- */

.composer {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  margin: 14px 16px 18px;
  overflow: hidden;
}

.composer:focus-within {
  border-color: #b8bec6;
}

.composer.is-dropping {
  border-color: var(--accent);
  border-style: dashed;
}

.composer__title {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  color: var(--text);
  font-weight: 600;
  min-height: 40px;
  padding: 9px 12px;
}

.composer__text {
  background: var(--panel);
  border: 0;
  color: var(--text);
  min-height: 104px;
  padding: 12px;
  resize: vertical;
  width: 100%;
}

.composer__text:focus-visible {
  outline: none;
}

.composer__foot {
  align-items: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
  padding: 9px 10px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 10px 10px;
}

.chips:empty {
  display: none;
}

.chip {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  font-size: 13px;
  gap: 8px;
  max-width: 100%;
  padding: 5px 5px 5px 8px;
}

.chip__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chip__size {
  color: var(--muted);
  flex: none;
}

.chip__cut {
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  flex: none;
  font-size: 17px;
  height: 26px;
  justify-content: center;
  line-height: 1;
  padding: 0;
  width: 26px;
}

.chip__cut:hover {
  background: var(--danger-soft);
  border-color: #f0b8b3;
  color: var(--danger);
}

/* ---------- Log ---------- */

.log {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 0;
  list-style: none;
  margin: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 0 16px 16px;
}

.log__empty {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 14px;
  padding: 14px;
}

.entry {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  padding: 0 0 16px;
  position: relative;
}

.entry:not(:last-child)::after {
  background: var(--border);
  bottom: 0;
  content: '';
  left: 13px;
  position: absolute;
  top: 29px;
  width: 1px;
}

.entry__avatar {
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 50%;
  display: inline-flex;
  grid-column: 1;
  height: 27px;
  justify-content: center;
  margin-top: 2px;
  position: relative;
  width: 27px;
  z-index: 1;
}

.entry__avatar::before {
  background: var(--accent);
  border-radius: 50%;
  content: '';
  height: 8px;
  width: 8px;
}

.entry__content {
  border-bottom: 1px solid var(--rule);
  grid-column: 2;
  min-width: 0;
  padding-bottom: 14px;
}

.entry:last-child .entry__content {
  border-bottom: 0;
}

.entry__head {
  align-items: center;
  display: flex;
  gap: 8px;
  min-height: 31px;
}

.entry__author {
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

.entry__when {
  color: var(--muted);
  font-size: 13px;
}

.entry__cut {
  align-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  font-size: 18px;
  height: 28px;
  justify-content: center;
  line-height: 1;
  margin-left: auto;
  opacity: 0.55;
  padding: 0;
  width: 28px;
}

.entry__cut:hover {
  background: var(--danger-soft);
  border-color: #f0b8b3;
  color: var(--danger);
  opacity: 1;
}

.entry__body {
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  font-size: 14px;
  margin: 6px 0 0;
  overflow-wrap: anywhere;
  padding: 10px 11px;
  white-space: pre-wrap;
}

.entry__files {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.file {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  display: flex;
  font-size: 13px;
  gap: 9px;
  min-height: 42px;
  padding: 7px 9px;
  text-decoration: none;
}

.file:hover {
  background: var(--sunken);
}

.file--image {
  padding: 5px 9px 5px 5px;
}

.file__mark {
  color: var(--muted);
  flex: none;
  height: 18px;
  width: 18px;
}

.file__thumb {
  border-radius: var(--radius);
  flex: none;
  height: 36px;
  object-fit: cover;
  width: 36px;
}

.file__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file__size {
  color: var(--muted);
  flex: none;
  margin-left: auto;
}

/* ---------- Toast ---------- */

.toast {
  background: var(--text);
  border-radius: var(--radius);
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  color: #ffffff;
  font-size: 13px;
  font-weight: 600;
  left: 50%;
  max-width: min(520px, calc(100vw - 32px));
  opacity: 0;
  padding: 10px 13px;
  pointer-events: none;
  position: fixed;
  transform: translate(-50%, 8px);
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 30;
}

.toast[data-tone='error'] {
  background: var(--danger);
  color: #ffffff;
}

.toast.is-up {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- Narrow screens ---------- */

@media (max-width: 900px) {
  :root {
    --gutter: 40px;
    --indent: 28px;
    --panel-w: 100vw;
    --content-pad: 10px;
  }

  .app {
    grid-template-columns: var(--content-pad) minmax(0, 1fr) var(--content-pad);
    grid-template-rows: auto var(--content-pad) minmax(0, 1fr) var(--content-pad);
  }

  .topbar {
    padding-left: 12px;
    padding-right: 12px;
  }

  .panel {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    bottom: var(--content-pad);
    box-shadow: 0 -14px 28px rgba(47, 52, 59, 0.16);
    left: var(--content-pad);
    max-height: calc(72dvh - var(--content-pad));
    position: fixed;
    right: var(--content-pad);
    width: auto;
    z-index: 5;
  }

  .rows {
    padding-bottom: 50dvh;
  }
}

@media (max-width: 560px) {
  :root {
    --gutter: 36px;
    --indent: 22px;
    --row-h: 56px;
  }

  .topbar__title {
    font-size: 17px;
  }

  .row__line {
    gap: 8px;
    padding-left: calc(6px + var(--depth, 0) * var(--indent));
    padding-right: 9px;
  }

  .row__twist {
    width: 24px;
  }

  .row__folder {
    width: 24px;
  }

  .row__folder-mark {
    height: 22px;
    width: 22px;
  }

  .row__name {
    font-size: 17px;
  }

  .row__count {
    font-size: 15px;
  }

  .row__add {
    height: 30px;
    width: 30px;
  }

  .panel__name {
    font-size: 19px;
  }

  .composer__foot .btn {
    flex: 1;
  }
}

/* ---------- Printing ---------- */

@media print {
  .topbar,
  .panel,
  .row__grip,
  .row__add,
  .root-add,
  .row__notes,
  .toast {
    display: none !important;
  }

  .app {
    display: block;
    height: auto;
  }

  .sheet {
    overflow: visible;
  }

  .row {
    break-inside: avoid;
  }
}
