:root {
  --bg: #f3f3f3;
  --ribbon-bg: #ffffff;
  --tab-bg: #f0f0f0;
  --active-tab: #2b579a;
  --active-tab-text: #fff;
  --group-sep: #d0d0d0;
  --border: #c0c0c0;
  --hover: #eaf5ff;
  --pressed: #c2e0ff;
  --page: #fff;
  --shadow: rgba(0, 0, 0, 0.15);
  --status-bg: #2b579a;
  --status-text: #fff;
  --accent: #2b579a;
  --font-ui: "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
  --font-body: "Calibri", "Arial", sans-serif;
  --page-w: 8.5in;
  --page-h: 11in;
  --margin: 0.75in;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; padding: 0; overflow: hidden; }
body {
  font-family: var(--font-ui);
  font-size: 14px;
  background: var(--bg);
  color: #333;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* Title bar */
#title-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  background: var(--active-tab);
  color: #fff;
  padding: 0 12px;
  flex: 0 0 auto;
  -webkit-app-region: drag;
}
.tb-left { display: flex; align-items: center; gap: 10px; }
.doc-icon {
  width: 24px; height: 24px; border-radius: 3px;
  background: #fff; color: var(--active-tab); font-weight: bold;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
}
.doc-info { display: flex; flex-direction: column; }
#doc-title {
  background: transparent; border: 1px solid transparent;
  color: #fff; font-size: 13px; width: 160px;
  padding: 2px 4px; border-radius: 2px;
}
#doc-title:focus { background: #fff; color: #333; border-color: #fff; outline: none; }
.save-state { font-size: 11px; opacity: 0.85; }

.qat { display: flex; align-items: center; gap: 2px; -webkit-app-region: no-drag; }
.qat-btn, .window-controls button {
  background: transparent; border: none; color: #fff;
  width: 32px; height: 28px; display: flex; align-items: center; justify-content: center;
  border-radius: 2px; cursor: pointer;
}
.qat-btn svg, .qat-btn { fill: currentColor; stroke: currentColor; }
.qat-btn svg { width: 18px; height: 18px; }
.qat-btn:hover, .window-controls button:hover { background: rgba(255,255,255,0.15); }
.qat-btn:active { background: rgba(255,255,255,0.3); }
.window-controls { display: flex; gap: 2px; -webkit-app-region: no-drag; }
.wc-close:hover { background: #e81123; }

/* Ribbon */
#ribbon { flex: 0 0 auto; background: var(--ribbon-bg); border-bottom: 1px solid var(--border); }
.ribbon-tabs { display: flex; background: var(--tab-bg); border-bottom: 1px solid var(--border); }
.ribbon-tab {
  border: none; background: transparent; padding: 6px 14px;
  font-size: 12px; color: #333; cursor: pointer; position: relative;
}
.ribbon-tab:hover { color: var(--active-tab); }
.ribbon-tab.active {
  background: var(--active-tab); color: var(--active-tab-text);
  border-top-left-radius: 3px; border-top-right-radius: 3px;
}

.ribbon-panels { position: relative; min-height: 120px; padding: 6px 12px; display: flex; align-items: stretch; }
.ribbon-panel { display: none; gap: 10px; flex-wrap: wrap; width: 100%; }
.ribbon-panel.active { display: flex; }

.group {
  display: flex; flex-direction: column; justify-content: flex-start; align-items: center;
  position: relative; padding: 4px 8px 4px 4px; border-right: 1px solid var(--group-sep);
}
.group:last-child { border-right: none; }
.group .stack { display: flex; flex-direction: column; gap: 6px; }

.split-btn { display: flex; align-items: center; border: 1px solid transparent; border-radius: 2px; }
.split-btn .main, .split-btn .arrow {
  border: 1px solid transparent; background: transparent; cursor: pointer;
  display: flex; align-items: center; gap: 4px; padding: 2px 4px; font-size: 12px;
}
.split-btn .arrow { padding: 2px; }
.split-btn:hover { border-color: var(--border); background: var(--hover); }
.split-btn .main:hover, .split-btn .arrow:hover { background: rgba(0,0,0,0.05); }
.split-btn svg { width: 18px; height: 18px; }
.cmd {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  border: 1px solid transparent; background: transparent; cursor: pointer;
  font-size: 12px; padding: 4px; border-radius: 2px; min-width: 48px;
}
.cmd svg { width: 24px; height: 24px; }
.cmd:hover, .icon-btn:hover, .toggle:hover, .mini-btn:hover { background: var(--hover); border-color: var(--border); }
.cmd:active, .icon-btn:active, .toggle.active, .toggle:active, .mini-btn:active { background: var(--pressed); }

.toggle, .icon-btn, .mini-btn {
  border: 1px solid transparent; background: transparent; cursor: pointer;
  border-radius: 2px; min-width: 28px; height: 28px; padding: 0 4px;
  display: inline-flex; align-items: center; justify-content: center; font-size: 12px;
}
.toggle svg, .icon-btn svg { width: 20px; height: 20px; }
.toggle.active { background: var(--pressed); border-color: #9cc3f4; }

.font-row, .toggle-row, .color-row, .align-row, .list-row { display: flex; align-items: center; gap: 3px; }
select { height: 26px; border: 1px solid var(--border); border-radius: 2px; font-size: 12px; background: #fff; }
#font-family { min-width: 120px; }
#font-size { min-width: 60px; }

.color-split .main { padding: 0 4px; }
.color-split .letter { font-weight: bold; font-size: 16px; line-height: 1; padding: 0 2px; }

.dialog-launcher {
  position: absolute; bottom: 2px; right: 4px; width: 10px; height: 10px;
  background: transparent; border: none; cursor: pointer; font-size: 9px; color: #666;
  padding: 0; line-height: 10px;
}
.dialog-launcher:hover { color: var(--active-tab); }

.styles-gallery { display: flex; flex-wrap: wrap; gap: 4px; max-width: 260px; }
.style-item {
  border: 1px solid var(--border); background: #fff; padding: 4px 8px;
  font-size: 12px; cursor: pointer; border-radius: 2px; min-width: 80px; text-align: left;
}
.style-item:hover { background: var(--hover); border-color: var(--active-tab); }
.style-item.active { background: var(--pressed); }
.view-group { flex-direction: row; gap: 6px; }
.view-btn span { display: block; font-size: 10px; }

.placeholder-text { color: #888; font-size: 12px; padding: 10px; }

/* Tooltips */
[title]:hover::after {
  content: attr(title); position: absolute; bottom: 120%; left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 4px 8px; border-radius: 3px; font-size: 11px;
  white-space: pre; z-index: 1000; pointer-events: none;
}

/* Ruler */
#ruler {
  height: 22px; background: #e9e9e9; border-bottom: 1px solid var(--border);
  display: flex; align-items: flex-end; overflow: hidden; flex: 0 0 auto;
}
.ruler-scale {
  width: var(--page-w); height: 100%; margin: 0 auto; position: relative;
  background: repeating-linear-gradient(90deg, #999 0, #999 1px, transparent 1px, transparent 0.125in),
              repeating-linear-gradient(90deg, #666 0, #666 2px, transparent 2px, transparent 0.5in);
  background-position: 0 bottom; background-repeat: repeat-x;
}

/* Workspace & page */
#workspace { flex: 1 1 auto; overflow: auto; background: var(--bg); padding: 20px 0; display: flex; justify-content: center; position: relative; }
.page-wrapper { display: flex; justify-content: center; transform-origin: top center; transition: transform 0.1s ease; }
.page {
  width: var(--page-w); min-height: var(--page-h); background: var(--page);
  box-shadow: 0 2px 8px var(--shadow), 0 0 1px rgba(0,0,0,0.08);
  padding: var(--margin); outline: none; font-family: var(--font-body); font-size: 11pt; line-height: 1.2;
  color: #222; overflow-wrap: break-word; margin: 0 auto;
}
.page p, .page h1, .page h2, .page h3, .page li { margin: 0 0 10px; }
.page h1 { font-size: 20pt; color: var(--active-tab); }
.page h2 { font-size: 16pt; color: #333; }
.page h3 { font-size: 13pt; }
.page blockquote { margin: 0 0 10px 0.5in; border-left: 3px solid #ccc; padding-left: 10px; color: #555; font-style: italic; }
.page ul, .page ol { margin: 0 0 10px 0; padding-left: 0.5in; }
.page li { margin-bottom: 4px; }
.page table { border-collapse: collapse; width: 100%; margin-bottom: 10px; }
.page td, .page th { border: 1px solid #aaa; padding: 4px 6px; min-width: 40px; }
.page img { max-width: 100%; height: auto; }
.page .page-break { page-break-after: always; border: none; border-top: 1px dashed #999; height: 0; margin: 12px 0; }
.page hr { border: none; border-top: 1px solid #ccc; margin: 10px 0; }

/* Views */
#app.web-layout .page {
  width: 100%; max-width: 100%; min-height: auto; box-shadow: none; padding: 12px; background: #fff;
}
#app.web-layout #workspace { padding: 0; background: #fff; }
#app.read-mode .page { width: 6in; font-size: 13pt; line-height: 1.6; background: #fffbf2; }
#app.read-mode #ribbon, #app.read-mode #ruler, #app.read-mode #status-bar { display: none; }
#app.read-mode #workspace { padding-top: 40px; }

/* Focus rings */
button:focus-visible, select:focus-visible, input:focus-visible, .page:focus { outline: 2px solid var(--active-tab); outline-offset: 1px; }

/* Status bar */
#status-bar {
  height: 28px; background: var(--status-bg); color: var(--status-text);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; font-size: 12px; flex: 0 0 auto;
}
.sb-left, .sb-right { display: flex; align-items: center; gap: 16px; }
.sb-btn { background: transparent; border: none; color: #fff; cursor: pointer; font-size: 14px; padding: 2px 4px; border-radius: 2px; }
.sb-btn:hover { background: rgba(255,255,255,0.15); }
.sb-btn.active { background: rgba(255,255,255,0.3); }
.zoom-wrap { display: flex; align-items: center; gap: 6px; }
#zoom-slider { width: 120px; }
#sb-sel-words { font-style: italic; opacity: 0.85; }

/* Backstage */
#backstage { position: fixed; inset: 0; background: rgba(0,0,0,0.2); z-index: 200; display: flex; }
#backstage.hidden { display: none; }
.bs-sidebar {
  width: 160px; background: #2b579a; color: #fff; display: flex; flex-direction: column; padding: 20px 0;
}
.bs-item {
  background: transparent; border: none; color: #fff; text-align: left;
  padding: 12px 20px; font-size: 14px; cursor: pointer;
}
.bs-item:hover, .bs-item.active { background: rgba(255,255,255,0.15); }
.bs-content { flex: 1; background: #fff; padding: 24px; overflow: auto; }
.bs-pane { display: none; }
.bs-pane.active { display: block; }
.bs-content h2 { margin-top: 0; color: var(--active-tab); }
.templates, .file-list { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 16px; }
.template, .file-item {
  border: 1px solid var(--border); border-radius: 4px; padding: 24px; width: 160px; text-align: center;
  background: #f9f9f9; cursor: pointer; font-size: 13px;
}
.template:hover, .file-item:hover { background: var(--hover); border-color: var(--active-tab); }
.bs-action {
  background: var(--active-tab); color: #fff; border: none; padding: 8px 16px; border-radius: 2px;
  cursor: pointer; margin-top: 12px;
}
.bs-action:hover { background: #1f3f6d; }
.bs-content label { display: block; margin: 12px 0; }
.bs-content input, .bs-content select { width: 260px; padding: 4px; border: 1px solid var(--border); }

/* Find bar */
#find-bar {
  position: fixed; bottom: 30px; right: 12px; background: #fff; border: 1px solid var(--border);
  display: flex; gap: 4px; padding: 4px; box-shadow: 0 2px 8px var(--shadow); z-index: 150;
}
#find-bar.hidden { display: none; }
#find-input { width: 200px; border: 1px solid var(--border); padding: 3px; }
.find-btn { width: 28px; height: 26px; border: 1px solid var(--border); background: #f0f0f0; cursor: pointer; }
.find-btn:hover { background: var(--hover); }

/* Mini toolbar */
#mini-toolbar {
  position: fixed; background: #fff; border: 1px solid var(--border);
  box-shadow: 0 2px 8px var(--shadow); display: flex; gap: 2px; padding: 3px; border-radius: 3px;
  z-index: 120; transition: opacity 0.2s, transform 0.2s; opacity: 0; transform: translateY(10px);
}
#mini-toolbar.visible { opacity: 1; transform: translateY(0); }
#mini-toolbar.hidden { display: none; }
.mini-btn { width: 28px; height: 26px; font-weight: bold; }

/* Context menu */
#context-menu {
  position: fixed; background: #fff; border: 1px solid var(--border);
  box-shadow: 0 2px 10px var(--shadow); min-width: 160px; z-index: 130;
  display: flex; flex-direction: column; padding: 4px 0; border-radius: 3px;
}
#context-menu.hidden { display: none; }
.ctx-item { background: transparent; border: none; text-align: left; padding: 6px 16px; cursor: pointer; font-size: 13px; }
.ctx-item:hover { background: var(--hover); }
.ctx-sep { border-top: 1px solid var(--border); margin: 4px 0; }

/* Popups (color, font, spacing) */
.popup {
  position: fixed; background: #fff; border: 1px solid var(--border); border-radius: 3px;
  box-shadow: 0 2px 10px var(--shadow); padding: 8px; z-index: 140; min-width: 160px; max-height: 320px; overflow: auto;
}
.popup.hidden { display: none; }
.color-grid { display: grid; grid-template-columns: repeat(10, 20px); gap: 2px; }
.color-swatch { width: 20px; height: 20px; border: 1px solid var(--border); cursor: pointer; }
.color-swatch:hover { border-color: #333; }
.font-option { padding: 4px 8px; cursor: pointer; font-size: 13px; }
.font-option:hover { background: var(--hover); }
.spacing-option { padding: 6px 8px; cursor: pointer; font-size: 13px; }
.spacing-option:hover { background: var(--hover); }

/* Dialog overlay */
#dialog-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.35); display: flex; align-items: center; justify-content: center; z-index: 210; }
#dialog-overlay.hidden { display: none; }
.dialog { background: #fff; border: 1px solid var(--border); border-radius: 4px; width: 420px; max-width: 90vw; padding: 16px; box-shadow: 0 4px 16px var(--shadow); }
.dialog h3 { margin-top: 0; color: var(--active-tab); }
.dialog-body { margin: 16px 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions button { padding: 6px 14px; border: 1px solid var(--border); background: #f0f0f0; cursor: pointer; border-radius: 2px; }
.dialog-actions button:first-child { background: var(--active-tab); color: #fff; border-color: var(--active-tab); }

/* Print styles */
@media print {
  #title-bar, #ribbon, #ruler, #status-bar, #backstage, #find-bar, #mini-toolbar, #context-menu, .popup { display: none !important; }
  html, body { overflow: visible; height: auto; background: #fff; }
  #app { display: block; height: auto; overflow: visible; }
  #workspace { padding: 0; overflow: visible; background: #fff; display: block; }
  .page-wrapper { transform: none !important; }
  .page { box-shadow: none; width: 100%; min-height: auto; margin: 0; padding: 0.75in; page-break-before: always; }
  .page:first-of-type { page-break-before: auto; }
  .page .page-break { page-break-after: always; border: none; }
}

/* Mobile / responsive */
@media (max-width: 860px) {
  :root { --page-w: 100%; --page-h: auto; --margin: 16px; }
  #ribbon { min-height: 0; }
  .ribbon-panels { min-height: 0; padding: 4px; }
  .ribbon-panel { gap: 4px; }
  .group { padding: 2px; border-right: none; }
  .cmd span { display: none; }
  .cmd svg { width: 22px; height: 22px; }
  .styles-gallery { max-width: 180px; }
  .style-item { min-width: 60px; font-size: 10px; }
  #ruler { display: none; }
  .page { width: 100%; min-height: auto; box-shadow: none; padding: 16px; }
  .page-wrapper { width: 100%; }
  .qat { display: none; }
  #title-bar { padding: 0 6px; }
  #doc-title { width: 120px; }
  #status-bar { font-size: 11px; padding: 0 6px; }
  #zoom-slider { width: 80px; }
}
