:root {
  --bg: #eef2f8;
  --panel: #ffffff;
  --panel-soft: #f7f9fc;
  --text: #0f172a;
  --muted: #64748b;
  --line: #d9e1ec;
  --brand: #2459b5;
  --brand-deep: #18428a;
  --brand-soft: #e9f1ff;
  --shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", Arial, sans-serif;
  background: linear-gradient(180deg, #edf2ff 0%, var(--bg) 40%, #f5f7fb 100%);
  color: var(--text);
}
body.dark {
  --bg: #0f172a;
  --panel: #111827;
  --panel-soft: #162133;
  --text: #e5edf7;
  --muted: #94a3b8;
  --line: #253247;
  --brand: #2e6ae0;
  --brand-deep: #214ea7;
  --brand-soft: #14284f;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.35);
  background: linear-gradient(180deg, #0b1220 0%, #111827 100%);
}

.app-shell {
  height: 100vh;
  display: grid;
  grid-template-rows: auto auto auto auto 1fr auto;
}

.topbar {
  background: linear-gradient(180deg, var(--brand) 0%, var(--brand-deep) 100%);
  color: white;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand-logo {
  width: 34px;
  height: 34px;
  object-fit: contain;
  border-radius: 10px;
  background: rgba(255,255,255,0.08);
  padding: 4px;
}
.doc-meta { min-width: 0; }
.doc-title {
  width: min(280px, 45vw);
  max-width: 100%;
  border: none;
  background: transparent;
  color: white;
  font-size: 15px;
  font-weight: 600;
  outline: none;
}
.doc-sub {
  font-size: 11px;
  color: rgba(255,255,255,0.82);
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.search-wrap input {
  width: min(260px, 42vw);
  border: 1px solid rgba(255,255,255,0.18);
  background: rgba(255,255,255,0.12);
  color: white;
  border-radius: 999px;
  padding: 8px 12px;
  outline: none;
}
.search-wrap input::placeholder { color: rgba(255,255,255,0.72); }
.icon-btn, .tool {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  min-height: 32px;
  padding: 0 10px;
  cursor: pointer;
  transition: 0.18s ease;
}
.icon-btn {
  background: rgba(255,255,255,0.12);
  color: white;
  border-color: rgba(255,255,255,0.18);
  min-width: 34px;
}
.icon-btn:hover,
.tool:hover,
.tab:hover { transform: translateY(-1px); }
.tool.active,
.tab.active,
.tool.primary {
  background: var(--brand-soft);
  color: var(--brand-deep);
  border-color: rgba(36, 89, 181, 0.18);
}
body.dark .tool.active,
body.dark .tab.active,
body.dark .tool.primary {
  color: #dbe7ff;
}

.tabbar {
  display: flex;
  gap: 2px;
  padding: 4px 8px 0;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.tab {
  border: 1px solid transparent;
  border-radius: 8px 8px 0 0;
  padding: 8px 12px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 600;
}
.tab.active {
  background: var(--panel-soft);
  color: var(--brand-deep);
  border-color: var(--line);
  border-bottom-color: var(--panel-soft);
}

.ribbon {
  display: none;
  background: var(--panel-soft);
  border-bottom: 1px solid var(--line);
  padding: 8px;
  gap: 8px;
  overflow-x: auto;
  white-space: nowrap;
}
.ribbon.active { display: flex; }
.group {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 8px 22px;
  position: relative;
  min-width: fit-content;
  box-shadow: 0 4px 18px rgba(15, 23, 42, 0.03);
}
.group-compact { padding-bottom: 22px; }
.group-doc-list { min-width: 210px; }
.group-label {
  position: absolute;
  bottom: 5px;
  left: 8px;
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.group-label.static {
  position: static;
  display: block;
  margin-bottom: 6px;
}
.row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.row + .row { margin-top: 6px; }
.wrap { flex-wrap: wrap; }
.select, .control {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  height: 32px;
  padding: 0 10px;
  outline: none;
}
.select.small { width: 72px; }
.full { width: 100%; }
.color-input {
  width: 34px;
  height: 32px;
  padding: 2px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
}
.readout {
  min-width: 54px;
  text-align: center;
  font-weight: 600;
  color: var(--muted);
}

.ruler-wrap {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 0 18px 8px;
}
.ruler-wrap.hidden { display: none; }
.ruler {
  max-width: 880px;
  margin: 0 auto;
  height: 28px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background:
    repeating-linear-gradient(
      to right,
      transparent 0,
      transparent 19px,
      rgba(100, 116, 139, 0.18) 19px,
      rgba(100, 116, 139, 0.18) 20px
    ),
    linear-gradient(180deg, var(--panel-soft), var(--panel));
  position: relative;
  overflow: hidden;
}
.ruler::after {
  content: "0    1    2    3    4    5    6    7    8    9   10   11   12";
  position: absolute;
  inset: 5px 12px auto 12px;
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 8px;
  white-space: nowrap;
}

.workspace {
  overflow: auto;
  padding: 18px;
}
.page-shell {
  max-width: 900px;
  margin: 0 auto;
  background: var(--panel);
  box-shadow: var(--shadow);
  border-radius: 20px;
  min-height: calc(100vh - 270px);
  padding: 22px 28px;
}
.editor {
  min-height: calc(100vh - 330px);
  outline: none;
  font-family: Arial, sans-serif;
  font-size: 16px;
  line-height: 1.7;
}
.editor img {
  max-width: 100%;
  border-radius: 12px;
}
.editor table {
  border-collapse: collapse;
  width: 100%;
  margin: 14px 0;
}
.editor table td, .editor table th {
  border: 1px solid var(--line);
  padding: 8px;
}
.editor mark.search-hit {
  background: #ffe082;
  color: #111827;
  border-radius: 4px;
  padding: 0 2px;
}
body.dark .editor mark.search-hit {
  background: #facc15;
  color: #111827;
}
.statusbar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
}
.status-left, .status-right {
  display: flex;
  gap: 8px;
  align-items: center;
}
.focus-mode .topbar,
.focus-mode .tabbar,
.focus-mode .ribbon,
.focus-mode .ruler-wrap,
.focus-mode .statusbar {
  display: none;
}
.focus-mode .app-shell {
  grid-template-rows: 1fr;
}
.focus-mode .workspace {
  padding: 10px;
}
.focus-mode .page-shell {
  min-height: calc(100vh - 20px);
}

@media (max-width: 900px) {
  .topbar {
    padding: 8px 10px;
    flex-wrap: wrap;
  }
  .search-wrap input {
    width: 100%;
  }
  .topbar-actions {
    width: 100%;
  }
  .search-wrap { flex: 1; }
  .page-shell {
    padding: 16px;
    border-radius: 16px;
  }
  .workspace {
    padding: 10px;
  }
  .editor {
    min-height: 62vh;
  }
}

@media (max-width: 640px) {
  .doc-title {
    width: 150px;
  }
  .brand-logo {
    width: 28px;
    height: 28px;
  }
  .ribbon {
    padding: 6px;
    gap: 6px;
  }
  .group {
    border-radius: 10px;
    padding: 6px 6px 20px;
  }
  .tool, .icon-btn, .select, .control {
    min-height: 30px;
    font-size: 12px;
  }
}
