*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f0f;
  --bg2: #1a1a1a;
  --bg3: #242424;
  --border: #2e2e2e;
  --accent: #4f8ef7;
  --accent-dim: #1a3a6b;
  --text: #e8e8e8;
  --text-dim: #888;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --orange: #f97316;
  --radius: 10px;
  --sidebar-w: 280px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  font-size: 14px;
}

/* ── AUTH ── */
#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg);
}
.auth-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px;
  width: 360px;
}
.auth-box h1 { font-size: 22px; margin-bottom: 6px; }
.auth-box .subtitle { color: var(--text-dim); font-size: 13px; margin-bottom: 28px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 10px 12px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 14px; outline: none;
}
.field input:focus { border-color: var(--accent); }
.btn-primary {
  width: 100%; padding: 11px; background: var(--accent);
  border: none; border-radius: 8px; color: #fff; font-size: 14px;
  font-weight: 600; cursor: pointer; transition: opacity .15s;
}
.btn-primary:hover { opacity: .9; }
.auth-error { color: var(--red); font-size: 13px; margin-top: 12px; text-align: center; min-height: 18px; }

/* ── APP LAYOUT ── */
#app { display: none; height: 100vh; flex-direction: row; }
#app.visible { display: flex; }

/* ── SIDEBAR ── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}
.sidebar-header {
  padding: 18px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-header .logo { font-size: 16px; font-weight: 700; letter-spacing: -.3px; }
.sidebar-header .user-line {
  font-size: 12px; color: var(--text-dim); margin-top: 4px;
  display: flex; justify-content: space-between; align-items: center;
}
.logout-btn {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 12px; padding: 2px 6px;
  border-radius: 4px; transition: background .15s;
}
.logout-btn:hover { background: var(--bg3); color: var(--text); }
.sidebar-search {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-search input {
  width: 100%; background: var(--bg3);
  border: 1px solid var(--border); border-radius: 7px;
  padding: 7px 10px; color: var(--text); font-size: 13px; outline: none;
}
.sidebar-search input:focus { border-color: var(--accent); }
.new-order-btn {
  margin: 10px 12px;
  padding: 9px; background: var(--accent);
  border: none; border-radius: 8px; color: #fff; font-size: 13px;
  font-weight: 600; cursor: pointer; width: calc(100% - 24px);
  transition: opacity .15s;
}
.new-order-btn:hover { opacity: .9; }

.order-list { flex: 1; overflow-y: auto; padding: 4px 0; }
.order-list::-webkit-scrollbar { width: 4px; }
.order-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.order-item {
  padding: 11px 14px; cursor: pointer;
  border-left: 3px solid transparent;
  transition: background .12s;
  position: relative;
}
.order-item:hover { background: var(--bg3); }
.order-item.active { background: var(--bg3); border-left-color: var(--accent); }
.order-item .num { font-size: 13px; font-weight: 600; }
.order-item .theme { font-size: 12px; color: var(--text-dim); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.order-item .status-row { display: flex; align-items: center; gap: 6px; margin-top: 5px; }
.status-badge {
  font-size: 11px; padding: 2px 7px; border-radius: 20px; font-weight: 500;
  white-space: nowrap;
}
.s-queued      { background: #1a2540; color: #7aabff; }
.s-planning    { background: #1a2d1a; color: #6ee787; }
.s-waiting     { background: #2d2200; color: #f5c543; }
.s-writing     { background: #1a2d1a; color: #6ee787; }
.s-done        { background: #0d2d1a; color: #22c55e; }
.s-revision    { background: #2d1a00; color: #f97316; }
.s-closed      { background: #222; color: #666; }
.s-natalia     { background: #2a1a40; color: #a78bfa; }

/* ── MAIN AREA ── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
}

/* Empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 12px;
}
.empty-state .icon { font-size: 48px; opacity: .4; }
.empty-state p { font-size: 15px; }

/* Order thread */
.order-thread { display: none; flex-direction: column; height: 100vh; }
.order-thread.visible { display: flex; }

.thread-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  background: var(--bg2);
}
.thread-header .order-info { flex: 1; min-width: 0; }
.thread-header .order-num { font-size: 18px; font-weight: 700; }
.thread-header .order-theme { color: var(--text-dim); font-size: 13px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-header .meta-row { display: flex; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.meta-tag {
  font-size: 11px; padding: 3px 8px; background: var(--bg3);
  border: 1px solid var(--border); border-radius: 6px; color: var(--text-dim);
}
.meta-tag strong { color: var(--text); }

.thread-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn-action {
  padding: 7px 14px; border-radius: 7px; border: 1px solid var(--border);
  background: var(--bg3); color: var(--text); font-size: 13px;
  cursor: pointer; transition: all .15s; white-space: nowrap;
}
.btn-action:hover { background: var(--bg); border-color: var(--accent); color: var(--accent); }
.btn-action.approve { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-action.approve:hover { opacity: .9; }
.btn-action.danger { border-color: var(--red); color: var(--red); }
.btn-action.danger:hover { background: #2d1010; }
.btn-action.warn { border-color: var(--orange); color: var(--orange); }
.btn-action.warn:hover { background: #2d1a00; }

.raf-status-bar {
  padding: 8px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 10px;
}
.raf-status-bar .raf-state { font-weight: 600; }

/* Messages */
.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.thread-messages::-webkit-scrollbar { width: 4px; }
.thread-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.msg {
  display: flex;
  gap: 10px;
  max-width: 80%;
}
.msg.from-manager { align-self: flex-end; flex-direction: row-reverse; }
.msg.from-system { align-self: center; max-width: 100%; }
.msg.from-raf { align-self: flex-start; }

.msg-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700; flex-shrink: 0;
}
.avatar-manager { background: var(--accent-dim); color: var(--accent); }
.avatar-raf { background: #1a3a2a; color: #22c55e; }
.avatar-system { background: var(--bg3); color: var(--text-dim); }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
  max-width: 100%;
}
.bubble-manager { background: var(--accent-dim); border-bottom-right-radius: 4px; }
.bubble-raf { background: var(--bg2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble-system {
  background: var(--bg3); color: var(--text-dim);
  font-size: 12px; padding: 6px 12px; border-radius: 20px;
  border: 1px solid var(--border);
}
.msg-time { font-size: 11px; color: var(--text-dim); margin-top: 4px; text-align: right; }
.msg.from-manager .msg-time { text-align: left; }

/* Modal */
.modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.7); z-index: 100;
  align-items: center; justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px; width: 480px; max-width: 95vw;
  max-height: 90vh; overflow-y: auto;
}
.modal h2 { font-size: 18px; margin-bottom: 20px; }
.modal-footer { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.btn-cancel {
  padding: 9px 18px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg3); color: var(--text); cursor: pointer; font-size: 14px;
}
.btn-cancel:hover { border-color: var(--accent); }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 9px 12px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 13px; outline: none;
  font-family: inherit;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  border-color: var(--accent);
}
.form-group textarea { resize: vertical; min-height: 80px; }
.form-group select option { background: var(--bg2); }

.radio-group { display: flex; gap: 10px; }
.radio-opt {
  flex: 1; padding: 10px; border: 1px solid var(--border);
  border-radius: 8px; cursor: pointer; text-align: center;
  transition: all .15s; user-select: none;
}
.radio-opt input { display: none; }
.radio-opt.selected { border-color: var(--accent); background: var(--accent-dim); color: var(--accent); }
.radio-opt .opt-label { font-size: 13px; font-weight: 600; }
.radio-opt .opt-desc { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.radio-opt.selected .opt-desc { color: var(--accent); opacity: .7; }

.file-drop {
  border: 2px dashed var(--border); border-radius: 8px;
  padding: 20px; text-align: center; cursor: pointer;
  transition: border-color .15s; color: var(--text-dim); font-size: 13px;
}
.file-drop:hover, .file-drop.drag { border-color: var(--accent); color: var(--accent); }
.file-list { margin-top: 8px; display: flex; flex-direction: column; gap: 4px; }
.file-chip {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 10px; background: var(--bg3); border-radius: 6px; font-size: 12px;
}
.file-chip .rm { cursor: pointer; color: var(--text-dim); margin-left: auto; }
.file-chip .rm:hover { color: var(--red); }

.form-error { color: var(--red); font-size: 12px; margin-top: 8px; min-height: 16px; }

/* Files in thread */
.file-attachment {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px; background: var(--bg3);
  border: 1px solid var(--border); border-radius: 7px;
  font-size: 12px; color: var(--text); text-decoration: none;
  margin-top: 6px;
}
.file-attachment:hover { border-color: var(--accent); color: var(--accent); }

/* Result files block */
.result-files {
  margin: 12px 20px;
  padding: 14px 16px;
  background: #0d2d1a;
  border: 1px solid #1a5c34;
  border-radius: 10px;
}
.result-files h4 { font-size: 13px; color: var(--green); margin-bottom: 10px; }
.result-file-link {
  display: flex; align-items: center; gap: 8px;
  color: var(--green); font-size: 13px; text-decoration: none;
  padding: 5px 0;
}
.result-file-link:hover { text-decoration: underline; }

/* Revision form inline */
.revision-form {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg2);
  display: none;
}
.revision-form.open { display: block; }
.revision-form textarea {
  width: 100%; background: var(--bg3); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); padding: 10px;
  font-size: 13px; font-family: inherit; resize: none; outline: none;
  min-height: 70px;
}
.revision-form textarea:focus { border-color: var(--orange); }
.revision-form-footer { display: flex; gap: 8px; margin-top: 8px; align-items: center; }

/* Adaptive */
@media (max-width: 640px) {
  .sidebar { width: 100%; position: fixed; z-index: 50; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: translateX(0); }
  .main { width: 100%; }
  .msg { max-width: 95%; }
  .modal { padding: 20px; }
}
