@font-face {
  font-family: "JetBrains Mono";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/JetBrainsMono-Regular.woff2") format("woff2");
}

:root,
:root[data-theme="dark"] {
  --bg: #161310;
  --surface: #1E1916;
  --code-bg: #120F0D;
  --text: #FFF8F3;
  --text-secondary: #F2E4D8;
  --text-muted: #C9B4A4;
  --border: #3D2F28;
  --accent: #E07040;
  --accent-emphasis: #F08A58;
  --accent-soft: rgba(224, 112, 64, 0.18);
  --tabbar-height: 52px;
  --ctl-h: 38px;
  --ui-size: 15px;
  --sidebar-width: 200px;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #FFFBF7;
  --surface: #FFFFFF;
  --code-bg: #FFF4EC;
  --text: #1A120E;
  --text-secondary: #3D2C24;
  --text-muted: #8A7064;
  --border: #EDD8CC;
  --accent: #D45C2C;
  --accent-emphasis: #C04E22;
  --accent-soft: rgba(212, 92, 44, 0.12);
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  height: 100%; margin: 0;
  background: var(--bg); color: var(--text);
  font-family: var(--font-kaiti);
}
::selection { background: var(--accent-soft); }
[hidden] { display: none !important; }

.app { height: 100vh; display: flex; overflow: hidden; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 16px 12px;
}
.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  flex: none;
}
.sidebar-folder {
  text-transform: none;
  letter-spacing: 0;
  font-size: 16px;
  font-weight: 650;
  color: var(--text);
}
.file-row {
  min-height: 40px;
  padding: 10px 16px;
  font-size: 15px;
}
.side-nav { flex: 1; overflow: auto; padding: 8px 0; }

.tabbar {
  height: var(--tabbar-height);
  display: flex;
  align-items: stretch;
  padding-right: 12px;
  gap: 8px;
}
.tabbar .tab {
  font-size: 16px;
  color: var(--text);
  font-weight: 650;
  cursor: default;
  padding: 0 20px;
}
.tabbar .spacer { flex: 1; }
.tabbar .btn { align-self: center; }

.btn {
  height: var(--ctl-h);
  padding: 0 16px;
  font-size: 15px;
  font-family: var(--font-kaiti);
}
.btn.primary { color: #fff; }
.btn.primary:hover { color: #fff; }

.scroll { flex: 1; overflow: auto; }
.page { padding: 28px 36px; max-width: 1100px; }

.md table { font-family: var(--font-kaiti); }
.md thead th {
  text-transform: none !important;
  letter-spacing: 0 !important;
  font-size: 13px;
  padding: 12px 14px;
}
.md tbody td { padding: 12px 14px; }
.md td.actions { white-space: normal; }

.muted { color: var(--text-muted); }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.copyish { cursor: pointer; }
.copyish:hover { color: var(--accent); }

.sheet-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center; z-index: 20;
}
.sheet-mask[hidden] { display: none; }
.xdialog-head { gap: 10px; }
.xdialog .body { padding: 16px; display: grid; gap: 14px; }
.field label {
  display: block;
  font-size: 13px;
  letter-spacing: 0;
  text-transform: none;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.field input, .field select, .field textarea {
  width: 100%; height: 40px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text); padding: 0 12px;
  font-family: var(--font-kaiti); font-size: 15px;
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--accent);
}
.field textarea { height: 72px; padding: 10px 12px; }
.settings-grid { display: grid; gap: 16px; max-width: 480px; }
.cred {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  font-size: 14px;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-all;
}
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  font-family: var(--font-kaiti);
}
.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--accent); color: #fff;
  border-radius: 6px;
  padding: 10px 16px;
  font-size: 15px;
  z-index: 40;
}

.nav-toggle, .tab-title, .nav-scrim { display: none; }

@media (max-width: 720px) {
  html, body, .app { height: 100dvh; }
  .app { flex-direction: column; }

  .nav-toggle, .tab-title { display: inline-flex; }
  .tab-title {
    align-items: center;
    font-size: 16px;
    font-weight: 650;
    padding: 0 4px;
    white-space: nowrap;
  }
  .nav-toggle {
    width: 40px;
    height: 40px;
    margin-left: 8px;
    font-size: 20px;
    color: var(--text);
  }

  .sidebar {
    position: fixed;
    z-index: 30;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 86vw);
    transform: translateX(-105%);
    transition: transform .2s ease;
    padding-top: env(safe-area-inset-top);
  }
  body.nav-open .sidebar { transform: none; }
  .nav-scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 25;
    background: rgba(0, 0, 0, .45);
  }
  body.nav-open { overflow: hidden; }
  .file-row { min-height: 44px; }

  .tabbar {
    height: auto;
    min-height: var(--tabbar-height);
    flex-wrap: wrap;
    padding: env(safe-area-inset-top) 8px 8px 0;
    gap: 6px;
  }
  .tabbar .btn {
    height: 36px;
    padding: 0 12px;
    font-size: 14px;
  }

  .page { padding: 16px 14px 24px; }
  .settings-grid { max-width: none; }

  .field input, .field select, .field textarea {
    font-size: 16px;
    min-height: 44px;
    height: 44px;
  }
  .field textarea { height: 88px; }

  .md-table-scroll { overflow: visible; }
  .md-table-scroll table {
    width: 100%;
    min-width: 0;
  }
  .md-table-scroll thead { display: none; }
  .md-table-scroll tbody,
  .md-table-scroll tr,
  .md-table-scroll td {
    display: block;
    width: 100%;
    white-space: normal;
    word-break: break-word;
  }
  .md-table-scroll tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 12px;
    padding: 12px 14px;
  }
  .md-table-scroll td {
    display: flex;
    gap: 10px;
    padding: 6px 0;
    align-items: flex-start;
  }
  .md-table-scroll td::before {
    content: attr(data-label);
    flex: 0 0 4.5em;
    color: var(--text-muted);
    font-size: 13px;
  }
  .md-table-scroll td.actions {
    display: block;
    padding-top: 10px;
  }
  .md-table-scroll td.actions::before { display: none; }
  .row-actions { gap: 8px; }
  .row-actions .btn {
    height: 36px;
    padding: 0 12px;
  }

  .sheet-mask {
    align-items: flex-end;
    padding: 0;
  }
  .xdialog {
    width: 100%;
    max-width: none;
    border-radius: 12px 12px 0 0;
    max-height: min(92dvh, 100%);
    overflow: auto;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .login-wrap {
    padding: 20px 16px;
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    align-items: stretch;
  }
  .login-wrap .xdialog {
    width: 100%;
    max-width: none;
    border-radius: 12px;
  }

  .toast {
    left: 16px;
    right: 16px;
    bottom: max(16px, env(safe-area-inset-bottom));
    text-align: center;
  }
}
