:root {
  color-scheme: light;
  --bg: #f4f6f5;
  --panel: #ffffff;
  --panel-muted: #eef2f0;
  --ink: #17201d;
  --muted: #66756f;
  --faint: #8a9691;
  --border: #d9e0dd;
  --border-strong: #9daca5;
  --accent: #1f7a5a;
  --accent-strong: #155f45;
  --accent-soft: #dceee7;
  --html: #245c8a;
  --markdown: #805a20;
  --warn: #946110;
  --shadow: 0 1px 2px rgba(23, 32, 29, 0.08);
  font-family: "Aptos", "Segoe UI", "Microsoft JhengHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

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

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.15;
}

h2 {
  font-size: 28px;
  line-height: 1.1;
}

h3 {
  font-size: 18px;
}

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

.login-page {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(90deg, rgba(31, 122, 90, 0.08) 1px, transparent 1px),
    linear-gradient(0deg, rgba(31, 122, 90, 0.08) 1px, transparent 1px),
    var(--bg);
  background-size: 32px 32px;
}

.login-panel {
  display: grid;
  grid-template-columns: minmax(220px, 320px) minmax(280px, 420px);
  width: min(760px, 100%);
  min-height: 420px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel);
  box-shadow: 0 18px 46px rgba(23, 32, 29, 0.12);
}

.login-brand {
  display: grid;
  align-content: space-between;
  gap: 24px;
  padding: 30px;
  color: #fff;
  background: #17382d;
}

.login-logo {
  width: 58px;
  height: 58px;
  padding: 8px;
  border-radius: 8px;
  background: #fff;
}

.login-form {
  display: grid;
  align-content: center;
  gap: 16px;
  padding: 32px;
}

.form-field {
  display: grid;
  gap: 6px;
}

.login-error {
  min-height: 20px;
  color: #9a3412;
  font-size: 13px;
  font-weight: 700;
}

.login-submit {
  width: 100%;
  justify-content: center;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.login-submit:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.login-submit:disabled {
  cursor: progress;
  opacity: 0.72;
}

.app-shell.sidebar-collapsed {
  grid-template-columns: 68px minmax(0, 1fr);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: 100vh;
  min-height: 100vh;
  padding: 18px;
  border-right: 1px solid var(--border);
  background: var(--panel);
}

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

.logo-button {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border: 0;
  border-radius: 6px;
  padding: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.logo-button:hover {
  background: var(--panel-muted);
}

.app-logo {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  display: block;
}

.app-title-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.sidebar-actions {
  display: flex;
  gap: 8px;
}

.sidebar-header,
.page-header,
.reader-actions,
.search-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-header,
.page-header {
  justify-content: space-between;
}

.sidebar-body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  flex: 1 1 auto;
  min-height: 0;
}

.sidebar-search {
  min-width: 0;
}

.sidebar-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.account-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fbfcfc;
}

.sidebar-account {
  flex: 0 0 auto;
  margin-top: auto;
}

.account-name {
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-logout {
  min-height: 32px;
  padding: 6px 9px;
  font-size: 13px;
}

.sidebar-header p,
.path-text,
.metadata-line,
.count-label,
.kb-meta,
.recent-meta,
.file-meta,
.folder-count {
  color: var(--muted);
  font-size: 12px;
}

.field-label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--ink);
  background: #fff;
}

.input:focus {
  border-color: var(--accent);
  outline: 2px solid var(--accent-soft);
}

.icon-button,
.tool-button,
.back-button,
.kb-item,
.recent-item {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  font-weight: 800;
}

.menu-icon,
.menu-icon::before,
.menu-icon::after {
  display: block;
  width: 16px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
  transition: transform 160ms ease, opacity 160ms ease;
}

.menu-icon {
  position: relative;
}

.menu-icon::before,
.menu-icon::after {
  content: "";
  position: absolute;
  left: 0;
}

.menu-icon::before {
  transform: translateY(-6px);
}

.menu-icon::after {
  transform: translateY(6px);
}

.history-icon {
  position: relative;
  width: 17px;
  height: 17px;
  border: 2px solid currentColor;
  border-radius: 50%;
}

.history-icon::before,
.history-icon::after {
  content: "";
  position: absolute;
  background: currentColor;
  border-radius: 999px;
}

.history-icon::before {
  left: 7px;
  top: 3px;
  width: 2px;
  height: 6px;
}

.history-icon::after {
  left: 7px;
  top: 8px;
  width: 5px;
  height: 2px;
}

.close-icon,
.close-icon::after {
  display: block;
  width: 17px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
}

.close-icon {
  transform: rotate(45deg);
}

.close-icon::after {
  content: "";
  transform: rotate(90deg);
}

.tool-button,
.back-button {
  min-height: 38px;
  padding: 8px 11px;
}

.back-button {
  width: max-content;
  color: var(--accent-strong);
}

.icon-button:hover,
.tool-button:hover,
.back-button:hover,
.kb-item:hover,
.recent-item:hover {
  border-color: var(--border-strong);
  background: var(--panel-muted);
}

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

.sidebar-collapsed .sidebar {
  align-items: center;
  gap: 12px;
  padding: 18px 10px;
}

.sidebar-collapsed .sidebar-header {
  display: grid;
  justify-items: center;
}

.sidebar-collapsed .sidebar-title {
  justify-content: center;
}

.sidebar-collapsed .app-title-copy,
.sidebar-collapsed .sidebar-body,
.sidebar-collapsed .sidebar-account,
.sidebar-collapsed #sidebarCloseButton {
  display: none;
}

.sidebar-collapsed .sidebar-actions {
  display: grid;
}

.sidebar-section {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  flex: 1 1 auto;
  gap: 8px;
  min-height: 0;
}

.sidebar-section h2 {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.kb-list,
.recent-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding-right: 2px;
}

.kb-item,
.recent-item {
  width: 100%;
  padding: 10px;
  color: var(--ink);
  text-decoration: none;
  text-align: left;
}

.kb-item {
  display: grid;
  gap: 5px;
}

.recent-item {
  display: grid;
  grid-template-columns: 4px minmax(0, 1fr);
  align-items: stretch;
  gap: 9px;
}

.recent-type {
  width: 4px;
  border-radius: 2px;
  background: currentColor;
}

.recent-item.html .recent-type {
  color: var(--html);
}

.recent-item.markdown .recent-type {
  color: var(--markdown);
}

.recent-main {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.recent-title,
.recent-meta {
  display: block;
  min-width: 0;
  max-width: 100%;
}

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

.kb-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.kb-name,
.recent-title,
.file-name {
  overflow-wrap: anywhere;
  font-weight: 750;
  line-height: 1.25;
}

.workspace {
  min-width: 0;
  min-height: 100vh;
}

.page {
  min-width: 0;
  min-height: 100vh;
}

.page-header {
  align-items: flex-end;
  gap: 20px;
  padding: 22px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.title-block {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.title-block h2,
.path-text {
  overflow-wrap: anywhere;
}

.eyebrow {
  color: var(--accent);
  font-size: 12px;
  font-weight: 850;
  text-transform: uppercase;
}

.summary-panel {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: #fbfcfc;
}

.metric {
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
}

.metric-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 850;
  text-transform: uppercase;
}

.metric-value {
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-tree {
  padding: 22px 28px 48px;
}

.tree-root {
  display: grid;
  gap: 6px;
  max-width: 1180px;
}

.tree-directory {
  min-width: 0;
}

.tree-directory summary {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 7px 10px 7px calc(10px + var(--depth) * 18px);
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  list-style: none;
}

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

.tree-directory summary:hover {
  border-color: var(--border);
  background: #fff;
}

.folder-marker {
  width: 10px;
  height: 10px;
  border-right: 2px solid var(--faint);
  border-bottom: 2px solid var(--faint);
  transform: rotate(-45deg);
  transition: transform 120ms ease;
}

.tree-directory[open] > summary .folder-marker {
  transform: rotate(45deg);
}

.folder-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}

.tree-children {
  display: grid;
  gap: 4px;
}

.tree-file {
  display: grid;
  grid-template-columns: 8px minmax(0, 1fr);
  align-items: stretch;
  gap: 12px;
  min-height: 46px;
  padding: 8px 10px 8px calc(10px + var(--depth) * 18px);
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--ink);
  text-decoration: none;
}

.tree-file:hover {
  border-color: var(--border-strong);
  background: #fff;
  box-shadow: var(--shadow);
}

.file-type {
  display: block;
  width: 4px;
  min-width: 0;
  margin-block: 3px;
  border-radius: 2px;
  background: currentColor;
  font-size: 0;
  line-height: 0;
}

.tree-file.html .file-type {
  color: var(--html);
}

.tree-file.markdown .file-type {
  color: var(--markdown);
}

.file-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.hidden {
  display: none !important;
}

.reader-page {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
}

.reader-toolbar {
  display: grid;
  grid-template-columns: minmax(240px, 460px) minmax(0, 1fr);
  align-items: end;
  gap: 16px;
  padding: 12px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.toolbar-group {
  min-width: 0;
}

.search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

.count-label {
  min-width: 42px;
  padding-top: 10px;
  text-align: right;
}

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

.content-shell {
  position: relative;
  min-height: 0;
  overflow: auto;
  background: #fff;
}

.empty-state {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 8px;
  min-height: 100%;
  padding: 28px;
  color: var(--muted);
  text-align: center;
}

.empty-state.inline {
  min-height: 280px;
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  background: #fff;
}

.document-view {
  display: none;
  width: 100%;
  min-height: 100%;
}

.document-view.active {
  display: block;
}

.html-document-host {
  display: block;
  min-height: 100%;
}

.markdown-view {
  max-width: 980px;
  margin: 0 auto;
  padding: 36px;
  line-height: 1.72;
  overflow-wrap: anywhere;
}

.markdown-view h1,
.markdown-view h2,
.markdown-view h3 {
  margin: 1.2em 0 0.45em;
}

.markdown-view h1:first-child,
.markdown-view h2:first-child,
.markdown-view h3:first-child {
  margin-top: 0;
}

.markdown-view p,
.markdown-view li,
.markdown-view pre {
  margin: 0 0 0.8em;
}

.markdown-view code,
.markdown-view pre {
  border-radius: 6px;
  background: #eef2f0;
}

.markdown-view code {
  padding: 2px 4px;
}

.markdown-view pre {
  overflow-x: auto;
  padding: 12px;
}

.highlight {
  background: #fff0a6;
  color: #2c2610;
}

.notice {
  border-left: 4px solid var(--warn);
  background: #fff8ed;
  color: #4c3515;
  padding: 12px;
  text-align: left;
}

@media (max-width: 1080px) {
  .summary-panel {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 860px) {
  .login-panel {
    grid-template-columns: 1fr;
    min-height: 0;
  }

  .login-brand {
    grid-template-columns: auto minmax(0, 1fr);
    align-content: center;
    align-items: center;
    padding: 22px;
  }

  .login-logo {
    width: 48px;
    height: 48px;
  }

  .login-form {
    padding: 24px;
  }

  .app-shell,
  .app-shell.sidebar-collapsed {
    grid-template-columns: 1fr;
  }

  .app-shell:not(.sidebar-collapsed)::after {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(23, 32, 29, 0.24);
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    width: min(86vw, 340px);
    height: 100dvh;
    min-height: 100dvh;
    max-height: none;
    overflow: hidden;
    border-right: 1px solid var(--border);
    border-bottom: 0;
    box-shadow: 10px 0 28px rgba(23, 32, 29, 0.18);
  }

  .icon-button:hover {
    border-color: var(--border);
    background: #fff;
  }

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

  .sidebar-collapsed .sidebar {
    position: relative;
    inset: auto;
    z-index: auto;
    width: auto;
    height: auto;
    min-height: auto;
    max-height: none;
    overflow: hidden;
    box-shadow: none;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    align-items: stretch;
    padding: 10px 18px;
  }

  .sidebar-collapsed .sidebar-header {
    display: flex;
    justify-content: space-between;
    width: 100%;
  }

  .sidebar-collapsed .sidebar-title {
    justify-content: flex-start;
  }

  .sidebar-collapsed .sidebar-actions {
    display: flex;
  }

  .reader-active.sidebar-collapsed .sidebar {
    min-height: 0;
    max-height: none;
    padding: 10px 18px;
  }

  .reader-active.sidebar-collapsed .sidebar-body {
    display: none;
  }

  .reader-active.sidebar-collapsed .sidebar-title p {
    display: none;
  }

  .reader-active.sidebar-collapsed .sidebar-header {
    min-height: 42px;
  }

  .page-header {
    align-items: stretch;
    flex-direction: column;
    padding: 18px;
  }

  .reader-page .page-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: start;
    gap: 14px;
    padding: 16px 18px;
  }

  .reader-page .title-block {
    grid-column: 1;
    min-width: 0;
  }

  .reader-page .reader-actions {
    grid-column: 2;
    grid-row: 1;
    justify-content: flex-end;
  }

  .reader-page .title-block h2 {
    font-size: 24px;
    line-height: 1.15;
  }

  .reader-actions {
    justify-content: flex-start;
  }

  .reader-toolbar {
    grid-template-columns: 1fr;
    padding-inline: 18px;
  }

  .library-page .summary-panel {
    display: none;
  }

  .library-page .page-header {
    display: none;
  }

  .file-tree {
    padding: 18px;
  }

  .tree-file {
    grid-template-columns: 8px minmax(0, 1fr);
  }

  .folder-count {
    display: none;
  }

  .markdown-view {
    padding: 24px 18px;
    font-size: 17px;
  }

  .markdown-view h1 {
    font-size: 26px;
    line-height: 1.18;
  }

  .markdown-view h2 {
    font-size: 22px;
    line-height: 1.22;
  }

  .markdown-view h3 {
    font-size: 19px;
    line-height: 1.25;
  }

  .markdown-view p,
  .markdown-view li {
    line-height: 1.68;
  }
}

@media (max-width: 560px) {
  .summary-panel {
    grid-template-columns: 1fr 1fr;
  }

  .tree-directory summary,
  .tree-file {
    padding-left: 10px;
  }
}
