:root {
  color-scheme: light dark;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #edf2f7;
  --surface-raised: #ffffff;
  --text: #17202a;
  --muted: #667085;
  --line: #d8dee7;
  --line-strong: #c5ceda;
  --accent: #176b5d;
  --accent-hover: #12554a;
  --accent-soft: #e5f2ef;
  --danger: #b42318;
  --danger-soft: #fff0ed;
  --code-bg: #111827;
  --code-text: #edf2ff;
  --shadow: 0 12px 32px rgba(16, 24, 40, 0.08);
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #171c22;
    --surface-soft: #202832;
    --surface-raised: #1d242c;
    --text: #e7ecf2;
    --muted: #9aa7b7;
    --line: #2d3743;
    --line-strong: #3a4654;
    --accent: #3fb49d;
    --accent-hover: #60c7b2;
    --accent-soft: #153a34;
    --danger: #ff8a7a;
    --danger-soft: #3b1f1d;
    --code-bg: #05080d;
    --code-text: #edf2ff;
    --shadow: 0 14px 36px rgba(0, 0, 0, 0.32);
  }
}

body[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #edf2f7;
  --surface-raised: #ffffff;
  --text: #17202a;
  --muted: #667085;
  --line: #d8dee7;
  --line-strong: #c5ceda;
  --accent: #176b5d;
  --accent-hover: #12554a;
  --accent-soft: #e5f2ef;
  --danger: #b42318;
  --danger-soft: #fff0ed;
  --code-bg: #111827;
  --code-text: #edf2ff;
  --shadow: 0 12px 32px rgba(16, 24, 40, 0.08);
}

body[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101418;
  --surface: #171c22;
  --surface-soft: #202832;
  --surface-raised: #1d242c;
  --text: #e7ecf2;
  --muted: #9aa7b7;
  --line: #2d3743;
  --line-strong: #3a4654;
  --accent: #3fb49d;
  --accent-hover: #60c7b2;
  --accent-soft: #153a34;
  --danger: #ff8a7a;
  --danger-soft: #3b1f1d;
  --code-bg: #05080d;
  --code-text: #edf2ff;
  --shadow: 0 14px 36px rgba(0, 0, 0, 0.32);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  white-space: nowrap;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
  padding: 9px 11px;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

textarea {
  resize: vertical;
}

label {
  display: grid;
  gap: 7px;
}

label span,
.search-box span {
  color: var(--muted);
  font-size: 12px;
}

.hidden {
  display: none !important;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 7px 13px;
  cursor: pointer;
}

.button:hover:not(:disabled) {
  border-color: var(--line-strong);
  background: var(--surface-soft);
}

.button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

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

.button.danger {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--line));
  background: var(--danger-soft);
  color: var(--danger);
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
}

.muted-text {
  color: var(--muted);
}

.auth-screen {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.auth-panel {
  display: grid;
  width: min(420px, 100%);
  gap: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-raised);
  padding: 28px;
  box-shadow: var(--shadow);
}

.auth-panel h1 {
  margin: -8px 0 4px;
  font-size: 28px;
  line-height: 1.2;
}

.error-line {
  min-height: 20px;
  margin: 0;
  color: var(--danger);
}

.app-shell {
  display: grid;
  grid-template-columns: 312px minmax(0, 1fr);
  min-height: 100vh;
  position: relative;
}

.sidebar-toggle,
.sidebar-backdrop {
  display: none;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  gap: 14px;
  min-height: 100vh;
  border-right: 1px solid var(--line);
  background: var(--surface-soft);
  padding: 20px;
  z-index: 30;
}

.brand {
  display: grid;
  gap: 4px;
}

.brand h1 {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  font-size: 28px;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand p,
.status-line,
#collection-meta,
.section-heading p {
  margin: 0;
  color: var(--muted);
}

.theme-switch {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 3px;
}

.theme-choice {
  min-height: 30px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.theme-choice.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
}

.sidebar-actions,
.sidebar-footer,
.toolbar,
.header-actions,
.dialog-actions,
.panel-title-row,
.entry-actions,
.user-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-actions .button,
.sidebar-footer .button {
  flex: 1;
}

.search-box {
  display: grid;
  gap: 7px;
}

.collection-list {
  display: grid;
  align-content: start;
  gap: 8px;
  overflow: auto;
  padding-right: 2px;
}

.collection-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 64px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  padding: 7px;
}

.collection-row:hover {
  background: color-mix(in srgb, var(--surface) 55%, transparent);
}

.collection-row.active {
  border-color: color-mix(in srgb, var(--accent) 32%, var(--line));
  background: var(--surface);
  box-shadow: 0 1px 0 rgba(16, 24, 40, 0.04);
}

.collection-item {
  display: grid;
  gap: 4px;
  min-width: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  padding: 7px 8px;
  text-align: left;
  cursor: pointer;
}

.collection-item:hover {
  background: transparent;
}

.collection-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.collection-item span {
  color: var(--muted);
  font-size: 12px;
}

.collection-order {
  display: grid;
  grid-template-columns: 28px;
  gap: 4px;
}

.order-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  min-height: 25px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--muted);
  padding: 0;
  cursor: pointer;
}

.order-button:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.order-button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.workspace {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 16px;
  min-width: 0;
  height: 100vh;
  padding: 24px;
  overflow: hidden;
}

.workspace-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  min-height: 76px;
}

.header-copy {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.header-copy h2 {
  margin: 0;
  overflow: hidden;
  font-size: 24px;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--line);
}

.tab {
  min-height: 38px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 0 12px;
  cursor: pointer;
}

.tab.active {
  border-bottom-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}

.view {
  display: none;
  min-height: 0;
}

.view.active {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
}

#markdown-view.active {
  grid-template-rows: minmax(0, 1fr);
}

.toolbar {
  justify-content: space-between;
  align-items: stretch;
  margin-bottom: 0;
}

.toolbar input {
  min-width: 0;
}

.entries-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 440px);
  gap: 16px;
  min-height: 0;
}

.entries-layout.form-closed {
  grid-template-columns: minmax(0, 980px);
}

.entry-list {
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.entry-card,
.editor-panel,
.users-card,
.markdown-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-raised);
  box-shadow: 0 1px 0 rgba(16, 24, 40, 0.03);
}

.entry-card {
  display: grid;
  gap: 7px;
  padding: 10px 12px;
}

.entry-card header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
}

.entry-card h3 {
  margin: 0 0 2px;
  overflow: hidden;
  font-size: 16px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-card p {
  margin: 0;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-card pre {
  margin: 0;
  overflow: hidden;
  border-radius: 6px;
  background: var(--code-bg);
  color: var(--code-text);
  padding: 7px 10px;
  white-space: nowrap;
  text-overflow: ellipsis;
  word-break: normal;
}

.entry-actions {
  flex-wrap: nowrap;
}

.entry-actions .button {
  min-height: 30px;
  padding: 4px 9px;
}

.editor-panel {
  display: grid;
  align-content: start;
  gap: 13px;
  padding: 16px;
}

.panel-title-row {
  justify-content: space-between;
}

.panel-title-row h3,
.section-heading h3,
.dialog-body h3 {
  margin: 0;
  font-size: 18px;
  line-height: 1.25;
}

.markdown-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  height: 100%;
  padding: 14px;
}

#markdown-editor {
  min-height: 0;
  height: 100%;
  font-family: Consolas, "Cascadia Mono", monospace;
  line-height: 1.45;
  tab-size: 2;
}

.empty-state {
  display: grid;
  min-height: 180px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 55%, transparent);
  padding: 24px;
}

.users-page {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  min-height: 0;
}

.users-card {
  display: grid;
  align-content: start;
  gap: 14px;
  min-height: 0;
  padding: 16px;
}

.user-list {
  display: grid;
  align-content: start;
  gap: 8px;
  min-height: 0;
  overflow: auto;
}

.user-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 12px;
}

.user-row strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-row p {
  margin: 3px 0 0;
  color: var(--muted);
}

.user-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.new-user-card {
  align-self: start;
}

.check-row {
  display: flex;
  align-items: center;
}

.check-row input {
  width: auto;
}

.status-line {
  min-height: 20px;
}

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-raised);
  color: var(--text);
  padding: 0;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.35);
}

.dialog-body {
  display: grid;
  gap: 14px;
  width: min(420px, calc(100vw - 32px));
  padding: 18px;
}

.dialog-actions {
  justify-content: flex-end;
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar-toggle {
    display: inline-flex;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 35;
    box-shadow: var(--shadow);
  }

  .sidebar-backdrop {
    display: none;
  }

  body.sidebar-open .sidebar-backdrop {
    display: none;
  }

  .sidebar {
    display: none;
    position: fixed;
    inset: 0 auto 0 0;
    width: min(320px, calc(100vw - 56px));
    min-height: 100vh;
    border-right: 1px solid var(--line);
    border-bottom: 0;
    box-shadow: var(--shadow);
    z-index: 30;
  }

  body.sidebar-open .sidebar {
    display: grid;
  }

  .workspace {
    height: auto;
    min-height: 100vh;
    overflow: visible;
    padding: 68px 14px 16px;
  }

  .entries-layout,
  .users-page {
    grid-template-columns: 1fr;
  }

  .workspace-header,
  .user-row {
    grid-template-columns: 1fr;
  }

  .workspace-header {
    display: grid;
  }

  .header-actions,
  .user-actions {
    justify-content: flex-start;
  }
}

@media (max-width: 680px) {
  body {
    font-size: 13px;
  }

  .workspace {
    gap: 12px;
  }

  .workspace-header {
    min-height: 0;
  }

  .header-copy h2 {
    font-size: 21px;
  }

  .header-actions,
  .toolbar,
  .sidebar-actions,
  .sidebar-footer {
    flex-wrap: wrap;
  }

  .toolbar {
    display: grid;
    grid-template-columns: 1fr;
  }

  .button {
    min-height: 34px;
  }

  .entry-card,
  .editor-panel,
  .users-card,
  .markdown-panel {
    padding: 12px;
  }

  .entry-card header {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
  }

  .entry-actions .button {
    min-height: 28px;
    padding: 3px 7px;
  }

  .entry-card pre {
    padding: 6px 8px;
  }
}
