:root {
  color-scheme: light;
  --bg: #f7f3ea;
  --panel: #fffdf8;
  --panel-strong: #f0efe7;
  --ink: #22231f;
  --muted: #696d64;
  --line: #d8d5ca;
  --accent: #176b5f;
  --accent-ink: #ffffff;
  --danger: #a33a2a;
  --focus: #d4952a;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  overflow: hidden;
}

button,
.rich-editor {
  font: inherit;
}

button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  padding: 0 14px;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

button:focus-visible,
.rich-editor:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 55%, transparent);
  outline-offset: 2px;
}

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

.app-shell {
  display: grid;
  grid-template-columns: minmax(250px, 320px) minmax(0, 1fr);
  height: 100%;
  min-height: 100%;
  overflow: hidden;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  gap: 12px;
  min-width: 0;
  border-right: 1px solid var(--line);
  background: var(--panel-strong);
  padding: 16px;
  overflow: hidden;
}

.sidebar-footer {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.sidebar-footer button {
  width: 100%;
}

.sidebar-header,
.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  min-width: 0;
}

.now-playing {
  display: grid;
  grid-template-columns: minmax(70px, 150px) minmax(180px, 280px);
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.now-playing[hidden] {
  display: none;
}

.now-playing span {
  color: var(--muted);
  font-size: 0.82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.now-playing audio {
  width: 100%;
  height: 34px;
}

.render-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--muted);
  padding: 0 10px;
  font-size: 0.88rem;
  white-space: nowrap;
}

.render-toggle input {
  accent-color: var(--accent);
}

.nav-button {
  width: 40px;
  min-width: 40px;
  padding: 0;
  font-weight: 700;
}

.sidebar-toggle {
  display: none;
}

.sidebar-close {
  display: none;
}

.file-title {
  min-width: 0;
}

h1,
p {
  margin: 0;
}

h1 {
  font-size: 1.15rem;
  line-height: 1.2;
}

.version-label,
#folderLabel,
.save-state {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.88rem;
  overflow-wrap: anywhere;
}

.version-label {
  font-size: 0.78rem;
}

.file-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.file-actions button {
  padding: 0 8px;
}

.status {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--muted);
  padding: 9px 10px;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

.status.error {
  border-color: color-mix(in srgb, var(--danger) 50%, var(--line));
  color: var(--danger);
}

.file-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  overflow: auto;
}

.file-item {
  width: 100%;
  min-height: 42px;
  border-color: transparent;
  background: transparent;
  padding-left: calc(10px + var(--tree-depth, 0) * 16px);
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.folder-item {
  color: var(--accent);
  font-weight: 650;
}

.file-item.selected-folder:not(.active) {
  background: color-mix(in srgb, var(--focus) 13%, transparent);
}

.image-file::before {
  color: var(--muted);
  content: "image ";
}

.audio-file::before {
  color: var(--muted);
  content: "audio ";
}

.text-file::before {
  color: var(--muted);
  content: "text ";
}

.unsupported-file {
  color: var(--muted);
  opacity: 0.82;
}

.file-item.active {
  border-color: color-mix(in srgb, var(--accent) 35%, var(--line));
  background: color-mix(in srgb, var(--accent) 11%, var(--panel));
  color: var(--accent);
  font-weight: 600;
}

.file-item.drop-target {
  border-color: var(--focus);
  background: color-mix(in srgb, var(--focus) 18%, var(--panel));
}

.editor-panel {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  min-width: 0;
  padding: 16px;
  background: var(--panel);
  overflow: hidden;
}

.current-file {
  font-weight: 650;
  overflow-wrap: anywhere;
}

.restore-access-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line));
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 8%, #ffffff);
  padding: 12px;
}

.restore-access-panel[hidden] {
  display: none;
}

.restore-access-panel strong {
  display: block;
  font-size: 0.95rem;
}

.restore-access-panel p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.86rem;
}

.restore-access-panel button {
  flex: 0 0 auto;
}

.rich-editor {
  width: 100%;
  height: 100%;
  min-height: 0;
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  line-height: 1.5;
  overflow: auto;
  padding: 14px;
  tab-size: 2;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.rich-editor[contenteditable="false"] {
  background: #f6f5f0;
  color: var(--muted);
}

.rich-editor[data-mode="plain"] {
  font-family: Consolas, "Cascadia Mono", monospace;
}

.rich-editor:empty::before {
  color: var(--muted);
  content: attr(data-placeholder);
}

.inline-asset-frame {
  display: block;
  position: relative;
  width: 100%;
  max-width: 100%;
  margin: 10px 0;
  padding-right: 12px;
}

.inline-asset {
  display: block;
  width: 100%;
  max-height: 70vh;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f6f5f0;
  object-fit: contain;
}

.inline-asset-resize-handle {
  position: absolute;
  top: 0;
  right: 0;
  width: 8px;
  height: 100%;
  min-height: 36px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 35%, var(--line));
  cursor: ew-resize;
  touch-action: none;
}

.inline-asset-frame:hover .inline-asset-resize-handle,
.inline-asset-frame.resizing .inline-asset-resize-handle {
  background: var(--accent);
}

.inline-audio-frame,
.audio-preview-frame,
.inline-file-frame {
  display: grid;
  gap: 6px;
  margin: 10px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f6f5f0;
  padding: 10px;
}

.inline-audio-frame span,
.audio-preview-frame span,
.inline-file-frame span {
  color: var(--muted);
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}

.inline-file-frame strong {
  color: var(--ink);
  font-size: 0.95rem;
  overflow-wrap: anywhere;
}

.inline-file-frame.linked-text-file {
  cursor: pointer;
}

.inline-file-frame.linked-text-file:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
  background: color-mix(in srgb, var(--accent) 7%, #f6f5f0);
}

.inline-audio-frame audio,
.audio-preview-frame audio {
  width: 100%;
}

.missing-asset {
  display: block;
  margin: 10px 0;
  border: 1px dashed var(--danger);
  border-radius: 6px;
  color: var(--danger);
  padding: 10px;
}

.rich-editor[data-mode="image"] {
  display: grid;
  place-items: center;
  white-space: normal;
}

.image-preview-frame {
  display: grid;
  width: 100%;
  height: 100%;
  margin: 0;
  place-items: center;
}

.image-preview {
  display: block;
  max-width: 100%;
  max-height: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  object-fit: contain;
}

.rich-editor[data-mode="audio"] {
  display: grid;
  align-content: start;
  white-space: normal;
}

.settings-dialog {
  width: min(420px, calc(100vw - 28px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  padding: 0;
  box-shadow: 0 18px 60px rgba(34, 35, 31, 0.22);
}

.settings-dialog::backdrop {
  background: rgba(34, 35, 31, 0.32);
}

.settings-panel {
  display: grid;
  gap: 16px;
  padding: 16px;
}

.settings-header,
.settings-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.settings-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.setting-field {
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 0.92rem;
}

.setting-check {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.92rem;
}

.setting-check input {
  accent-color: var(--accent);
}

.setting-field input {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  color: var(--ink);
  font: inherit;
  padding: 0 10px;
}

@media (max-width: 760px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr);
    position: relative;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(320px, 88vw);
    border-right: 1px solid var(--line);
    border-bottom: 0;
    box-shadow: 16px 0 34px rgba(34, 35, 31, 0.18);
    transform: translateX(-100%);
    transition: transform 160ms ease;
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .editor-panel,
  .sidebar {
    padding: 8px;
  }

  .editor-header {
    align-items: center;
    gap: 6px;
  }

  .restore-access-panel {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
  }

  .restore-access-panel button {
    width: 100%;
  }

  .file-title {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    column-gap: 8px;
    flex: 1;
  }

  .file-title .save-state {
    grid-column: 2;
  }

  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .sidebar-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .current-file {
    font-size: 0.9rem;
    line-height: 1.15;
  }

  #folderLabel,
  .save-state {
    margin-top: 1px;
    font-size: 0.72rem;
  }

  .header-actions {
    justify-content: flex-end;
    gap: 6px;
  }

  .header-actions button,
  .nav-button,
  .render-toggle {
    min-height: 34px;
  }

  .nav-button {
    width: 34px;
    min-width: 34px;
  }

  .render-toggle {
    padding: 0 7px;
    font-size: 0.78rem;
  }

  #refreshButton {
    padding: 0 8px;
    font-size: 0.82rem;
  }

  .rich-editor {
    margin-top: 8px;
    padding: 10px;
  }

  .now-playing {
    grid-template-columns: 1fr;
    order: 5;
    width: 100%;
  }

  .now-playing audio {
    height: 30px;
  }
}
