:root {
  --drawio-bg: #f9f9f9;
  --drawio-border: #cfcfcf;
  --drawio-border-dark: #b7b7b7;
  --drawio-text: #333333;
  --drawio-muted: #666666;
  --drawio-blue: #3b73d9;

  --codex-bg: #111111;
  --codex-panel: #171717;
  --codex-panel-2: #1f1f1f;
  --codex-panel-3: #2b2b2b;
  --codex-border: #2d2d2d;
  --codex-border-soft: #242424;
  --codex-text: #e7e7e7;
  --codex-muted: #8f8f8f;
  --codex-muted-2: #6f6f6f;
  --codex-accent: #f06f3d;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, Helvetica, sans-serif;
  background: var(--drawio-bg);
  color: var(--drawio-text);
  font-size: 12px;
}
button, textarea { font-family: inherit; font-size: 12px; }
code { font-family: Consolas, "SFMono-Regular", "Courier New", monospace; font-size: 11px; }

.app-shell {
  height: 100vh;
  width: 100vw;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  overflow: hidden;
  background: var(--drawio-bg);
}
.app-shell.panel-collapsed { grid-template-columns: minmax(0, 1fr); }

.draw-area {
  grid-column: 1;
  height: 100vh;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.topbar {
  height: 36px;
  flex: 0 0 36px;
  background: #141414;
  border-bottom: 1px solid var(--codex-border-soft);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px 0 12px;
  color: var(--codex-text);
  font-size: 12px;
}
.topbar-left,
.topbar-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-left strong {
  color: #f0f0f0;
  font-weight: 600;
  letter-spacing: .01em;
}
.topbar span { color: var(--codex-muted-2); margin-left: 0; }
.topbar code { color: #bdbdbd; }
.topbar a {
  height: 26px;
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  border-radius: 8px;
  color: #cfcfcf;
  text-decoration: none;
  white-space: nowrap;
}
.topbar a:hover { color: #ffffff; background: #242424; text-decoration: none; }
.topbar-toggle {
  height: 26px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid #454545;
  border-radius: 8px;
  background: #2b2b2b;
  color: #ffffff;
  cursor: pointer;
  padding: 0 10px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04);
}
.topbar-toggle:hover { background: #2f2f2f; border-color: #424242; }
.topbar-toggle-dot { font-size: 10px; color: var(--codex-accent); }
.app-shell.panel-collapsed .topbar-toggle { background: transparent; color: #a8a8a8; }
.app-shell.panel-collapsed .topbar-toggle-dot { color: #707070; }
iframe {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  border: 0;
  background: white;
}

.chat-panel {
  grid-column: 2;
  height: 100vh;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  background: var(--codex-bg);
  color: var(--codex-text);
  border-left: 1px solid var(--codex-border);
  display: flex;
  flex-direction: column;
}
.app-shell.panel-collapsed .chat-panel { display: none; }

.panel-titlebar {
  flex: 0 0 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 10px 0 14px;
  border-bottom: 1px solid var(--codex-border-soft);
  background: #141414;
}
.panel-title-left { display: flex; align-items: center; min-width: 0; overflow: hidden; }
.panel-title {
  min-width: 0;
  color: #f0f0f0;
  font-weight: 600;
  line-height: 36px;
  letter-spacing: .01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.panel-title span {
  margin-left: 8px;
  color: var(--codex-muted-2);
  font-weight: 500;
}
.panel-subtitle { display: none; }
.panel-toggle {
  width: 26px;
  height: 26px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: #9a9a9a;
  cursor: pointer;
  font-size: 17px;
  line-height: 22px;
  padding: 0;
}
.panel-toggle:hover { color: #ffffff; background: #242424; border-color: #343434; }


.sessions-panel {
  position: fixed;
  z-index: 70;
  top: 0;
  left: 0;
  width: 300px;
  max-width: min(86vw, 340px);
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #111111;
  color: var(--codex-text);
  border-right: 1px solid var(--codex-border);
  box-shadow: 18px 0 44px rgba(0,0,0,.32);
  transform: translateX(0);
  transition: transform .18s ease;
}
.app-shell.sessions-collapsed .sessions-panel {
  transform: translateX(calc(-100% - 4px));
  pointer-events: none;
}
.sessions-titlebar {
  flex: 0 0 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 10px 0 12px;
  border-bottom: 1px solid var(--codex-border-soft);
  background: #141414;
}
.sessions-titlebar strong {
  color: #f0f0f0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .01em;
}
.sessions-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}
.session-action-btn {
  width: 26px;
  height: 26px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: #a2a2a2;
  cursor: pointer;
  font-size: 17px;
  line-height: 22px;
  padding: 0;
}
.session-action-btn:hover { color: #fff; background: #242424; border-color: #343434; }
.chat-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 8px;
}
.chat-list-item {
  width: 100%;
  min-height: 56px;
  display: grid;
  gap: 4px;
  align-content: center;
  text-align: left;
  border: 1px solid transparent;
  border-radius: 11px;
  background: transparent;
  color: var(--codex-text);
  cursor: pointer;
  padding: 9px 10px;
}
.chat-list-item:hover { background: #1f1f1f; border-color: #2b2b2b; }
.chat-list-item.active { background: #27211e; border-color: #47352d; }
.chat-list-title {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  color: #f0f0f0;
}
.chat-list-meta {
  display: block;
  color: var(--codex-muted-2);
  font-size: 11px;
}
.sessions-open-btn .topbar-toggle-dot { color: #bdbdbd; }

.show-panel-btn {
  position: fixed;
  z-index: 20;
  top: 7px;
  right: 8px;
  display: none;
  height: 24px;
  padding: 2px 9px;
  border: 1px solid #a9a9a9;
  border-radius: 7px;
  background: linear-gradient(#ffffff, #e8e8e8);
  color: #222;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,.18);
}
.app-shell.panel-collapsed .show-panel-btn { display: none; }

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 26px 14px 18px;
  background: var(--codex-bg);
  scrollbar-gutter: stable;
}
.messages::-webkit-scrollbar { width: 10px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: #303030; border-radius: 999px; border: 3px solid var(--codex-bg); }
.msg { max-width: 100%; margin: 0 0 22px; }
.msg-label { display: none; }
.msg-body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.48;
  font-size: 13px;
  color: var(--codex-text);
}
.msg.user {
  display: flex;
  justify-content: flex-end;
}
.msg.user .msg-body {
  max-width: min(84%, 520px);
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--codex-panel-3);
  border: 1px solid #303030;
  box-shadow: 0 1px 0 rgba(255,255,255,.03) inset;
}
.msg.assistant .msg-body {
  max-width: 100%;
  padding: 0;
  background: transparent;
  border: 0;
}
.msg.error .msg-body {
  padding: 12px 14px;
  border-radius: 12px;
  background: #2a1616;
  border: 1px solid #693131;
  color: #ffb7b7;
}

.composer {
  flex: 0 0 auto;
  margin: 0 14px 14px;
  padding: 0;
  display: grid;
  gap: 0;
  background: var(--codex-panel-3);
  border: 1px solid #333333;
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.22), inset 0 1px 0 rgba(255,255,255,.04);
  overflow: hidden;
}
textarea {
  width: 100%;
  resize: none;
  min-height: 72px;
  max-height: 180px;
  border: 0;
  background: transparent;
  color: var(--codex-text);
  padding: 13px 13px 7px;
  outline: none;
  line-height: 1.42;
}
textarea::placeholder { color: #7d7d7d; }
textarea:disabled { opacity: .72; }
.composer-row {
  display: grid;
  grid-template-columns: 25px minmax(0, 1fr) auto 28px;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 5px 8px 8px;
}
.status {
  color: var(--codex-accent);
  font-size: 12px;
  min-width: 0;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status::before {
  content: "◈";
  display: inline-block;
  margin-right: 5px;
  font-size: 10px;
  color: var(--codex-accent);
}
.icon-btn,
.secondary-btn,
.send-btn {
  border: 0;
  cursor: pointer;
  flex: 0 0 auto;
}
.icon-btn {
  width: 25px;
  height: 25px;
  border-radius: 8px;
  background: transparent;
  color: #a2a2a2;
  font-size: 17px;
  line-height: 25px;
  padding: 0;
}
.icon-btn:hover { background: #363636; color: #f0f0f0; }
.secondary-btn {
  height: 25px;
  padding: 0 8px;
  border-radius: 8px;
  background: transparent;
  color: #a0a0a0;
  white-space: nowrap;
}
.secondary-btn:hover { background: #363636; color: #f0f0f0; }
.send-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  background: #d9d9d9;
  color: #161616;
  font-size: 18px;
  line-height: 28px;
  padding: 0;
}
.send-btn:hover { background: #ffffff; }
.send-btn:disabled,
.secondary-btn:disabled { opacity: .5; cursor: wait; }

@media (max-width: 900px) {
  html,
  body,
  .app-shell,
  .draw-area {
    height: 100dvh;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
  }

  .app-shell {
    display: block;
    grid-template-columns: none;
    grid-template-rows: none;
  }

  .draw-area {
    width: 100%;
    min-height: 0;
  }

  .topbar {
    height: 34px;
    flex-basis: 34px;
    padding: 0 8px 0 10px;
  }
  .topbar-left span,
  #openDrawio { display: none; }
  .topbar-right { gap: 6px; }
  .topbar-toggle {
    height: 26px;
    min-width: 42px;
    justify-content: center;
    padding: 0 10px;
  }
  .topbar-toggle-dot { display: none; }

  .chat-panel {
    position: fixed;
    inset: 0;
    z-index: 50;
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    border: 0;
    border-left: 0;
    border-top: 0;
    box-shadow: none;
  }
  .app-shell.panel-collapsed .chat-panel { display: none; }
  .app-shell:not(.panel-collapsed) .chat-panel { display: flex; }

  .panel-titlebar {
    height: 44px;
    flex-basis: 44px;
    padding: max(0px, env(safe-area-inset-top)) 10px 0 12px;
  }
  .panel-title {
    line-height: 44px;
    font-size: 13px;
  }
  .panel-title span { display: none; }
  .panel-toggle {
    width: auto;
    min-width: 94px;
    height: 30px;
    padding: 0 10px;
    border-color: #343434;
    border-radius: 9px;
    background: #242424;
    color: #f0f0f0;
    font-size: 12px;
    line-height: 28px;
    white-space: nowrap;
  }

  .messages {
    padding: 18px 12px 14px;
    scrollbar-gutter: auto;
  }
  .msg { margin-bottom: 18px; }
  .msg.user .msg-body { max-width: 90%; }

  .composer {
    margin: 0 10px max(10px, env(safe-area-inset-bottom));
    border-radius: 14px;
    max-width: calc(100% - 20px);
  }
  textarea {
    min-height: 68px;
    max-height: 34dvh;
    font-size: 16px;
    line-height: 1.35;
    -webkit-text-size-adjust: 100%;
  }
  .composer-row { min-height: 40px; }
  .status { font-size: 11px; }

  .app-shell.panel-collapsed .show-panel-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .show-panel-btn {
    top: auto;
    right: 14px;
    bottom: max(14px, env(safe-area-inset-bottom));
    z-index: 40;
    height: 44px;
    min-width: 54px;
    padding: 0 15px;
    border: 1px solid #363636;
    border-radius: 999px;
    background: #141414;
    color: #ffffff;
    font-weight: 600;
    box-shadow: 0 8px 24px rgba(0,0,0,.28);
  }

  .sessions-panel {
    width: min(86vw, 320px);
    height: 100dvh;
  }
  .sessions-titlebar {
    height: 44px;
    flex-basis: 44px;
    padding: max(0px, env(safe-area-inset-top)) 10px 0 12px;
  }
}
