:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-2: #f7f7f7;
  --line: #e9e9e9;
  --line-strong: #d7d7d7;
  --text: #111111;
  --muted: #737373;
  --muted-2: #a3a3a3;
  --bubble-me: #ffffff;
  --bubble-them: #f3f3f3;
  --danger: #b42318;
  --shadow: 0 24px 80px rgba(0,0,0,.14);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body { overflow: hidden; -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }

.app-shell {
  min-height: 100dvh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0,1fr) auto;
  background: #fff;
}

.topbar {
  position: relative;
  z-index: 20;
  min-height: 82px;
  padding: max(14px, env(safe-area-inset-top)) 18px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.92);
  backdrop-filter: blur(18px);
  overflow: hidden;
}
.topbar::before,
.topbar::after {
  content: "";
  position: absolute;
  pointer-events: none;
  border-radius: 999px;
  opacity: .7;
}
.topbar::before {
  width: 72px;
  height: 8px;
  top: 14px;
  left: calc(50% - 108px);
  background: linear-gradient(90deg, transparent, #ededed, transparent);
}
.topbar::after {
  width: 5px;
  height: 5px;
  top: 19px;
  left: calc(50% + 92px);
  background: #d7d7d7;
  box-shadow: 10px 8px 0 #efefef, -7px 15px 0 #f3f3f3;
}

.avatar-cluster {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.avatar-button {
  position: relative;
  width: 46px;
  height: 46px;
  padding: 2px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform .16s ease, background .16s ease;
}
.avatar-button:hover { background: #f5f5f5; }
.avatar-button:active { transform: scale(.95); }
.avatar-button:focus-visible { outline: 2px solid #111; outline-offset: 2px; }
.avatar-display {
  width: 46px;
  height: 46px;
  padding: 2px;
  border-radius: 50%;
  display: grid;
  place-items: center;
}
.user-avatar-display { cursor: default; }
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  background: #f3f3f3;
  border: 1px solid #dedede;
  color: #111;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .06em;
  box-shadow: 0 4px 14px rgba(0,0,0,.04);
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.user-avatar {
  background: #111;
  border-color: #111;
  color: #fff;
}
.user-avatar svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.avatar-connector {
  width: 14px;
  height: 1px;
  background: #d8d8d8;
  position: relative;
}
.avatar-connector::after {
  content: "";
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #bdbdbd;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 3px #fff;
}
.role-presence {
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #111;
  border: 2px solid #fff;
  transition: .18s ease;
}
.role-avatar-button.is-busy .role-presence {
  background: #fff;
  border-color: #111;
  animation: presencePulse 1s ease-in-out infinite;
}
@keyframes presencePulse { 50% { transform: scale(.7); opacity: .45; } }

.sheet-dialog {
  width: min(660px, calc(100vw - 18px));
  margin: auto auto 8px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
}
.sheet-dialog::backdrop { background: rgba(0,0,0,.28); backdrop-filter: blur(4px); }
.sheet {
  max-height: min(88dvh, 820px);
  overflow: auto;
  border-radius: 28px;
  padding: 10px 18px 24px;
  background: #fff;
  border: 1px solid #e3e3e3;
  box-shadow: var(--shadow);
}
.sheet-handle { width: 38px; height: 4px; border-radius: 99px; background: #d7d7d7; margin: 1px auto 15px; }
.sheet-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.sheet-head h2 { margin: 2px 0 0; font-size: 24px; font-weight: 700; letter-spacing: -.035em; }
.text-button { border: 0; background: transparent; color: #111; padding: 9px 0 3px 14px; cursor: pointer; font-weight: 600; }

.role-list { display: grid; gap: 8px; }
.role-option {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 18px;
  padding: 13px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}
.role-option.active { border-color: #111; }
.role-option .meta { display: grid; gap: 3px; min-width: 0; }
.role-option .meta strong { font-size: 14px; }
.role-option .meta small { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.role-option .check { margin-left: auto; color: #111; }

.secondary-wide-button {
  width: 100%;
  min-height: 44px;
  margin-top: 12px;
  border: 1px solid #111;
  background: #fff;
  color: #111;
  border-radius: 14px;
  font-weight: 650;
  cursor: pointer;
}

.settings-sheet { display: grid; gap: 12px; }
.settings-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  padding: 4px;
  border-radius: 14px;
  background: #f4f4f4;
  margin-bottom: 2px;
}
.settings-tab {
  min-height: 36px;
  border: 0;
  border-radius: 11px;
  background: transparent;
  color: #777;
  cursor: pointer;
  font-size: 12px;
  font-weight: 650;
}
.settings-tab.active { background: #fff; color: #111; box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.settings-panel { display: grid; gap: 10px; }
.settings-card { border: 1px solid var(--line); border-radius: 20px; overflow: hidden; background: #fff; }
.settings-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 15px 10px; }
.settings-title-row > div:first-child { display: grid; gap: 3px; }
.settings-title-row strong { font-size: 14px; }
.settings-kicker { color: var(--muted-2); font-size: 9px; letter-spacing: .12em; }
.mini-actions { display: flex; gap: 6px; }
.mini-actions button {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: #111;
  font-size: 18px;
  cursor: pointer;
}

.field-stack { display: grid; gap: 7px; border-top: 1px solid var(--line); padding: 13px 15px; }
.field-stack > span { color: #555; font-size: 10px; letter-spacing: .05em; }
.field-stack input:not([type="range"]), .field-stack textarea, .field-stack select {
  width: 100%;
  border: 1px solid #dedede;
  outline: 0;
  color: #111;
  background: #fff;
  border-radius: 11px;
  padding: 10px 11px;
  font-size: 13px;
}
.field-stack textarea { resize: none; line-height: 1.55; min-height: 88px; }
.field-stack input:focus, .field-stack textarea:focus, .field-stack select:focus { border-color: #111; }
.field-hint { color: var(--muted); font-size: 10px; line-height: 1.45; }
.range-label { display: flex; align-items: center; justify-content: space-between; }
.range-label b { color: #111; font-size: 11px; }
input[type="range"] { width: 100%; accent-color: #111; }

.preset-chips { display: flex; flex-wrap: wrap; gap: 7px; }
.preset-chip {
  border: 1px solid #dcdcdc;
  background: #fafafa;
  color: #333;
  border-radius: 999px;
  padding: 7px 10px;
  font-size: 10px;
  cursor: pointer;
}
.preset-chip:active { background: #efefef; }

.model-row { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
.small-primary-button {
  border: 1px solid #111;
  background: #111;
  color: #fff;
  border-radius: 11px;
  padding: 0 13px;
  font-size: 11px;
  font-weight: 650;
  cursor: pointer;
}
.small-primary-button:disabled { opacity: .45; cursor: default; }

.two-column-fields { display: grid; grid-template-columns: 110px minmax(0,1fr); }
.two-column-fields .field-stack + .field-stack { border-left: 1px solid var(--line); }

.primary-button {
  width: calc(100% - 24px);
  margin: 12px;
  border: 1px solid #111;
  background: #111;
  color: #fff;
  border-radius: 14px;
  min-height: 44px;
  font-weight: 680;
  cursor: pointer;
}
.row-button {
  width: 100%;
  border: 0;
  border-top: 1px solid var(--line);
  background: #fff;
  color: #111;
  min-height: 62px;
  padding: 11px 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  text-align: left;
  cursor: pointer;
}
.row-button:first-of-type { border-top: 0; }
.row-button span:first-child { display: grid; gap: 3px; }
.row-button strong { font-size: 13px; }
.row-button small { color: var(--muted); font-size: 10px; line-height: 1.45; }
.row-button:disabled { opacity: .45; cursor: not-allowed; }
.row-button.danger strong, .row-button.danger > span:last-child { color: var(--danger); }
.security-note { margin: 4px 4px 0; color: #888; font-size: 10px; line-height: 1.6; }
.security-note code { color: #333; }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(108px + env(safe-area-inset-bottom));
  z-index: 60;
  max-width: min(88vw, 520px);
  transform: translate(-50%, 14px);
  opacity: 0;
  pointer-events: none;
  background: #111;
  color: #fff;
  border-radius: 999px;
  padding: 9px 13px;
  font-size: 11px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,.18);
  transition: .22s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 720px) {
  body { display: grid; place-items: center; background: #f2f2f2; }
  .app-shell { width: min(100vw, 560px); height: min(100dvh, 940px); min-height: 0; border-left: 1px solid #ddd; border-right: 1px solid #ddd; box-shadow: 0 20px 80px rgba(0,0,0,.08); }
}


/* v4 detail pass */
.sheet-eyebrow {
  display: block;
  margin-bottom: 3px;
  color: var(--muted-2);
  font-size: 9px;
  letter-spacing: .16em;
  font-weight: 750;
}
.user-avatar-button { cursor: pointer; }
.user-avatar-button:hover { background: #f5f5f5; }
.user-avatar.has-image { background: #fff; border-color: #dedede; }
.avatar-preview { width: 48px; height: 48px; font-size: 13px; }
.profile-card .settings-title-row { min-height: 70px; }
.hidden-file { display: none !important; }
.avatar-source-grid { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
.small-ghost-button {
  min-height: 38px;
  border: 1px solid #d7d7d7;
  background: #fafafa;
  border-radius: 11px;
  padding: 0 12px;
  color: #222;
  font-size: 11px;
  font-weight: 650;
  cursor: pointer;
}
.small-ghost-button:active { transform: scale(.98); background: #f1f1f1; }
.role-avatar-editor { gap: 9px; }
.avatar-editor-row { display: grid; grid-template-columns: 58px minmax(0,1fr); gap: 12px; align-items: center; }
.avatar-editor-row .avatar-preview { width: 54px; height: 54px; }
.avatar-editor-fields { display: grid; gap: 7px; min-width: 0; }
.avatar-editor-fields > input { width: 100%; }
.avatar-editor-actions { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 7px; }
.avatar-editor-actions input { min-width: 0; }
.field-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.field-heading > span { color: #555; font-size: 10px; letter-spacing: .05em; }
.field-heading button {
  border: 0;
  background: transparent;
  color: #777;
  padding: 3px 0 3px 10px;
  font-size: 10px;
  cursor: pointer;
}
.field-heading button:hover { color: #111; }
.tool-strip {
  display: grid;
  grid-template-columns: repeat(5, minmax(0,1fr));
  gap: 6px;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: linear-gradient(180deg, #fff, #fafafa);
}
.tool-strip button {
  min-width: 0;
  min-height: 58px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: #555;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 4px;
  cursor: pointer;
  font-size: 9px;
}
.tool-strip button:hover { background: #f1f1f1; color: #111; }
.tool-strip button b { color: #111; font-size: 17px; line-height: 1; font-weight: 500; }
.tool-strip button span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

@media (max-width: 460px) {
  .tool-strip { grid-template-columns: repeat(5, minmax(0,1fr)); gap: 2px; padding: 5px; }
  .tool-strip button { min-height: 55px; font-size: 8px; }
  .two-column-fields { grid-template-columns: 1fr 1fr; }
}

/* v5 library + message tooling */
.topbar::before,
.topbar::after { display: none; }
.topbar-meta {
  position: absolute;
  bottom: 12px;
  color: #b1b1b1;
  font-size: 7px;
  font-weight: 750;
  letter-spacing: .13em;
  line-height: 1;
  pointer-events: none;
}
.topbar-meta-left { left: 18px; }
.topbar-meta-right { right: 18px; }

.prompt-order {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 12px 0;
  padding: 9px 10px;
  border: 1px dashed #dedede;
  border-radius: 12px;
  background: #fafafa;
  color: #777;
  font-size: 7px;
  font-weight: 800;
  letter-spacing: .1em;
}
.prompt-order span { color: #333; }
.prompt-order i { color: #b8b8b8; font-style: normal; }

.library-dialog .sheet { max-height: min(92dvh, 900px); }
.library-toolbar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
}
.library-toolbar button {
  min-height: 38px;
  border: 1px solid #dcdcdc;
  border-radius: 11px;
  background: #fafafa;
  color: #333;
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
}
.library-toolbar button:active { background: #efefef; transform: scale(.99); }
.library-toolbar button:disabled { opacity: .35; cursor: default; }
.library-dialog textarea { min-height: 250px; }
.danger-outline-button {
  width: calc(100% - 24px);
  min-height: 40px;
  margin: 0 12px 12px;
  border: 1px solid #e4c4c0;
  border-radius: 13px;
  background: #fff;
  color: var(--danger);
  font-size: 11px;
  font-weight: 680;
  cursor: pointer;
}
.danger-outline-button:disabled { opacity: .32; cursor: default; }

.compact-dialog { width: min(520px, calc(100vw - 18px)); }
.compact-sheet { padding-bottom: 16px; }
.compact-head { margin-bottom: 12px; }
.compact-head h2 { font-size: 20px; }
.message-action-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
}
.message-action-grid button {
  min-width: 0;
  min-height: 70px;
  border: 1px solid #e2e2e2;
  border-radius: 15px;
  background: #fafafa;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 5px;
  cursor: pointer;
}
.message-action-grid button b {
  color: #222;
  font-size: 8px;
  letter-spacing: .1em;
}
.message-action-grid button span { color: #666; font-size: 10px; }
.message-action-grid button.danger-action b,
.message-action-grid button.danger-action span { color: var(--danger); }
.edit-message-field { border: 1px solid var(--line); border-radius: 16px; }
.edit-message-field textarea { min-height: 160px; }

@media (max-width: 390px) {
  .topbar-meta { font-size: 6px; letter-spacing: .1em; }
  .message-action-grid { grid-template-columns: repeat(2, 1fr); }
  .message-action-grid button { min-height: 58px; }
}
