:root {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --row-alt: #fafaf7;
  --border: #e7e5e4;
  --border-strong: #d6d3d1;
  --text: #1c1917;
  --text-muted: #78716c;
  --accent: #0f766e;
  --accent-hover: #115e59;
  --danger: #b91c1c;
  --warn: #dc2626;

  --status-작업전: #94a3b8;
  --status-작업중: #0ea5e9;
  --status-초안: #a855f7;
  --status-수정: #f59e0b;
  --status-대기보류: #64748b;
  --status-완료: #10b981;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Pretendard Variable', Pretendard, -apple-system, system-ui, sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.muted.small { font-size: 12px; }
.spacer { flex: 1; }

/* ---------- 로그인 ---------- */
.login {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
}
.login-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 40px 36px; width: 360px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  text-align: center;
}
.login-box h1 { margin: 0 0 8px; font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.login-box p { margin: 0 0 24px; font-size: 13px; }
.login-box .btn { width: 100%; padding: 11px; font-size: 14px; }
.error { color: var(--danger); font-size: 12px; margin-top: 12px !important; }

/* ---------- 상단바 ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.logo { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.02em; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.user-name { font-size: 13px; color: var(--text-muted); }
.user-name .admin-tag {
  display: inline-block; margin-left: 6px; padding: 2px 6px;
  background: var(--accent); color: white; border-radius: 4px;
  font-size: 10.5px; font-weight: 600;
}

/* ---------- 필터바 ---------- */
.filterbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border); gap: 16px; flex-wrap: wrap;
}
.tabs { display: flex; gap: 0; background: var(--bg); padding: 3px; border-radius: 8px; }
.tab {
  border: 0; background: transparent; padding: 6px 14px; border-radius: 6px;
  font-size: 13px; font-weight: 500; color: var(--text-muted); cursor: pointer;
  font-family: inherit; position: relative;
}
/* 탭 사이 연한 세로선 */
.tab + .tab::before {
  content: "";
  position: absolute; left: 0; top: 20%; height: 60%;
  width: 1px; background: var(--border-strong);
}
/* 활성탭 앞뒤 세로선 숨김 */
.tab.active::before,
.tab.active + .tab::before { opacity: 0; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,0.06); }

.filters { display: flex; gap: 8px; align-items: center; }
.input, .select {
  height: 32px; padding: 0 10px; border: 1px solid var(--border-strong);
  border-radius: 6px; background: var(--surface); font-size: 13px;
  font-family: inherit; color: var(--text);
}
.input { width: 260px; }
.input:focus, .select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

/* ---------- 버튼 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 14px; border-radius: 6px; font-size: 13px;
  font-weight: 500; font-family: inherit; cursor: pointer; border: 1px solid transparent;
  transition: background 0.12s, color 0.12s;
}
.btn.primary { background: var(--accent); color: white; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--border-strong); }
.btn.danger:hover { background: var(--danger); color: white; border-color: var(--danger); }
.btn.icon { width: 32px; padding: 0; background: transparent; border-color: transparent; color: var(--text-muted); }
.btn.icon:hover { background: var(--bg); color: var(--text); }

/* ---------- 내보내기 드롭다운 ---------- */
.export-wrap { position: relative; }
.export-menu {
  position: absolute; top: calc(100% + 4px); right: 0;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 8px; box-shadow: 0 8px 20px rgba(0,0,0,0.08);
  min-width: 140px; z-index: 50; overflow: hidden;
}
.export-menu button {
  display: block; width: 100%; text-align: left;
  border: 0; background: transparent; padding: 8px 12px;
  font-size: 13px; font-family: inherit; color: var(--text); cursor: pointer;
}
.export-menu button:hover { background: var(--bg); }

/* ---------- 테이블 ---------- */
.table-wrap { padding: 0 24px 40px; overflow-x: auto; }
table.orders {
  width: 100%; min-width: 960px; border-collapse: collapse; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
  margin-top: 16px; font-size: 12px;
}
.orders thead th {
  text-align: left; padding: 9px 8px; font-weight: 600; font-size: 11px;
  color: var(--text-muted); background: #f5f5f1; border-bottom: 1px solid var(--border-strong);
  border-right: 1px solid var(--border-strong);
  letter-spacing: 0.02em; white-space: nowrap;
}
.orders thead th:last-child { border-right: 0; }
.orders tbody td {
  padding: 9px 8px; border-bottom: 1px solid var(--border);
  border-right: 1px solid #e2e0de;
  vertical-align: middle; max-width: 200px;
}
.orders tbody td:last-child { border-right: 0; }

/* zebra stripe */
.orders tbody tr:nth-child(even) td { background: var(--row-alt); }
.orders tbody tr { cursor: pointer; transition: background 0.08s; }
.orders tbody tr:hover td { background: #f1f5f4 !important; }
.orders tbody tr:last-child td { border-bottom: 0; }

/* 첫 셀(플래그)에 담당자 컬러바 */
.orders tbody tr td:first-child {
  border-left: 4px solid transparent;
  text-align: center;
  padding: 10px 4px;
  width: 28px;
}
.orders.show-owner-color tbody tr td:first-child {
  border-left-color: var(--owner-color, transparent);
}

.owner-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  margin-right: 6px; vertical-align: middle; background: var(--owner-color, #d6d3d1);
}

/* 안 읽은 코멘트 빨간 점 */
.unread-flag {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  background: var(--warn); box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.15);
}

.col-flag { width: 16px; min-width: 16px; padding: 0; }
.col-owner { width: 72px; min-width: 72px; }
.col-channel { width: 60px; min-width: 55px; }
.col-date { width: 52px; min-width: 52px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.col-item { min-width: 80px; }
.col-qty { width: 48px; min-width: 44px; text-align: right; }
.col-spec { min-width: 70px; }
.col-content { min-width: 80px; }
.col-client { min-width: 80px; }
.col-contact { min-width: 100px; }
.col-vendor { min-width: 70px; }
.col-delivery { width: 52px; min-width: 52px; white-space: nowrap; }
.col-payment { min-width: 70px; }
.col-status { width: 82px; min-width: 82px; white-space: nowrap; }
.col-delivered { width: 60px; min-width: 60px; text-align: center; }
.delivered-check { width: 18px; height: 18px; cursor: pointer; accent-color: var(--accent); }
.col-printorder { width: 64px; min-width: 64px; text-align: center; }
.btn-printorder {
  border: 1px solid var(--accent); background: transparent; color: var(--accent);
  border-radius: 5px; padding: 3px 8px; font-size: 11.5px; font-weight: 600;
  cursor: pointer; font-family: inherit; white-space: nowrap;
}
.btn-printorder:hover { background: var(--accent); color: white; }
.cell-ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.cell-num { text-align: right; white-space: nowrap; }

.status-pill {
  display: inline-block; padding: 3px 9px; border-radius: 11px;
  font-size: 11.5px; font-weight: 500; color: white; white-space: nowrap;
}
.status-pill[data-s="작업전"] { background: var(--status-작업전); }
.status-pill[data-s="작업중"] { background: var(--status-작업중); }
.status-pill[data-s="초안"] { background: var(--status-초안); }
.status-pill[data-s="수정"] { background: var(--status-수정); }
.status-pill[data-s="대기,보류"] { background: var(--status-대기보류); }
.status-pill[data-s="완료"] { background: var(--status-완료); }

.empty {
  padding: 60px 20px; text-align: center; color: var(--text-muted);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  margin-top: 16px;
}

.footnote { font-size: 11.5px; margin: 12px 4px 0; }

/* ---------- 모달 ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  padding: 20px;
}
.modal-box {
  background: var(--surface); border-radius: 12px; width: 760px; max-height: 90vh;
  display: flex; flex-direction: column; box-shadow: 0 20px 50px rgba(0,0,0,0.2);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 24px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 16px; font-weight: 700; }
.modal-body { padding: 20px 24px; overflow-y: auto; flex: 1; }
.modal-foot {
  display: flex; gap: 8px; align-items: center; padding: 14px 24px;
  border-top: 1px solid var(--border);
}

.locked-notice {
  background: #fef3c7; color: #78350f; border: 1px solid #fcd34d;
  padding: 10px 14px; border-radius: 8px; font-size: 12.5px;
  margin-bottom: 16px;
}

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* admin-only: 기본 숨김, body.is-admin일 때만 표시 */
.admin-only { display: none !important; }
.is-admin .admin-only { display: flex !important; }
.admin-badge {
  display: inline-block; margin-left: 4px; padding: 1px 5px;
  background: var(--accent); color: white;
  border-radius: 3px; font-size: 9.5px; font-weight: 600;
  vertical-align: middle; letter-spacing: 0;
}

/* 폼 섹션 구분 타이틀 */
.form-section-title {
  font-size: 11px; font-weight: 700; color: var(--accent);
  text-transform: uppercase; letter-spacing: 0.07em;
  margin: 20px 0 8px; padding-bottom: 5px;
  border-bottom: 1px solid var(--border);
  grid-column: span 2;
}
.grid label {
  display: flex; flex-direction: column; gap: 5px; font-size: 12px;
  color: var(--text-muted); font-weight: 500;
}
.grid label.span2 { grid-column: span 2; }
.grid input, .grid select, .grid textarea {
  border: 1px solid var(--border-strong); border-radius: 6px; padding: 8px 10px;
  font-size: 13px; font-family: inherit; color: var(--text); background: var(--surface);
  resize: vertical;
}
.grid input:focus, .grid select:focus, .grid textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent);
}
.grid input:disabled, .grid select:disabled, .grid textarea:disabled {
  background: #f5f5f4; color: var(--text-muted); cursor: not-allowed;
}

/* ---------- 코멘트 ---------- */
.comments { margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border); }
.comments-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.comments-head h3 { margin: 0; font-size: 13.5px; font-weight: 700; }
.comments-list {
  display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px;
  max-height: 240px; overflow-y: auto;
}
.comment-item {
  background: var(--bg); border-radius: 8px; padding: 8px 12px;
  border-left: 3px solid transparent;
}
.comment-item.unread {
  border-left-color: var(--warn);
  background: #fef2f2;
}
.comment-head {
  display: flex; gap: 8px; align-items: baseline;
  font-size: 11.5px; margin-bottom: 3px;
}
.comment-head strong { font-weight: 600; }
.comment-head .time { color: var(--text-muted); }
.comment-text { font-size: 13px; color: var(--text); white-space: pre-wrap; word-break: break-word; }
.comments-empty { color: var(--text-muted); font-size: 12px; padding: 8px 0; }

.comment-read-btn {
  margin-left: auto;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--warn);
  font-size: 11px; font-weight: 600;
  padding: 2px 8px; border-radius: 4px;
  cursor: pointer; font-family: inherit;
}
.comment-read-btn:hover { background: var(--warn); color: white; border-color: var(--warn); }

/* ---------- 그룹 헤더 (담당자별 묶음) ---------- */
.orders tbody tr.group-header td {
  background: #ecf3f2 !important;
  border-top: 1px solid var(--border-strong);
  border-bottom: 1px solid var(--border-strong);
  padding: 8px 12px;
  font-weight: 700; font-size: 12.5px;
  color: var(--text);
  border-left: 4px solid var(--owner-color, #d6d3d1) !important;
}
.orders tbody tr.group-header { cursor: default; }
.orders tbody tr.group-header:hover td { background: #ecf3f2 !important; }
.orders tbody tr.group-header .count {
  font-weight: 500; color: var(--text-muted); margin-left: 6px;
}
.group-dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  background: var(--owner-color, #d6d3d1);
  margin-right: 8px; vertical-align: middle;
}

.card-group-header {
  display: flex; align-items: center;
  padding: 6px 4px; margin-top: 10px; margin-bottom: 2px;
  font-weight: 700; font-size: 13px;
  color: var(--text);
  border-left: 4px solid var(--owner-color, #d6d3d1);
  padding-left: 10px;
}
.card-group-header:first-child { margin-top: 0; }
.card-group-header .count { font-weight: 500; color: var(--text-muted); margin-left: 6px; font-size: 12px; }

.comments-input { display: flex; gap: 8px; }
.comments-input input {
  flex: 1; height: 32px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: 6px;
  font-size: 13px; font-family: inherit;
}
.comments-input input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

/* ---------- 멤버 관리 ---------- */
.section-title { margin: 0 0 6px; font-size: 13.5px; font-weight: 700; }
.member-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.member-row {
  display: grid; grid-template-columns: 1.3fr 1.2fr 100px auto auto;
  gap: 8px; align-items: center;
  padding: 8px 10px; background: var(--bg); border-radius: 8px;
}
.member-row .email { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-row .self-tag {
  display: inline-block; font-size: 10.5px; color: var(--accent); font-weight: 600;
}
.member-row input {
  height: 30px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: 6px;
  font-size: 13px; font-family: inherit; background: var(--surface);
}
.member-row input:disabled { background: #ebebe9; color: var(--text-muted); }
.member-row select {
  height: 30px;
}
.member-row .role-pill {
  display: inline-block; padding: 2px 8px; border-radius: 4px;
  font-size: 10.5px; font-weight: 600;
  background: var(--bg); color: var(--text-muted);
}
.member-row .role-pill.admin {
  background: var(--accent); color: white;
}
.member-row .btn { height: 30px; padding: 0 10px; font-size: 12px; }
.member-add-row {
  display: grid; grid-template-columns: 1fr 110px auto;
  gap: 8px; align-items: center; margin-top: 10px;
}
.member-add-row input {
  height: 32px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: 6px;
  font-size: 13px; font-family: inherit;
}

/* ---------- 토스트 ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1c1917; color: white; padding: 10px 18px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 10px 20px rgba(0,0,0,0.2); z-index: 200;
}

/* ============================================================
   모바일 카드 뷰 (768px 미만)
   ============================================================ */
.mobile-only { display: none !important; }
/* desktop-only는 원래 display 값을 유지 (table은 table, 그 외 block) */

.cards { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-left: 4px solid var(--owner-color, #d6d3d1);
  border-radius: 10px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 6px;
  cursor: pointer; transition: background 0.08s;
}
.card:active { background: var(--bg); }
.card-row1 {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--text-muted);
}
.card-row1 .unread-flag { margin-right: 2px; }
.card-row1 .owner-name { color: var(--text); font-weight: 600; }
.card-row1 .date { margin-left: auto; }
.card-item { font-size: 15px; font-weight: 600; color: var(--text); word-break: break-word; }
.card-sub {
  font-size: 12.5px; color: var(--text-muted);
  display: flex; flex-wrap: wrap; gap: 4px 10px;
}
.card-sub b { color: var(--text); font-weight: 500; }
.card-foot {
  display: flex; align-items: center; gap: 8px; margin-top: 2px;
}
.card-foot .status-pill { font-size: 11px; padding: 2px 8px; }
.card-foot .price { margin-left: auto; font-size: 13px; font-weight: 600; color: var(--text); }

/* Floating action button */
.fab {
  position: fixed; bottom: 20px; right: 20px;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: white; border: 0;
  font-size: 28px; font-weight: 300; line-height: 1;
  box-shadow: 0 6px 18px rgba(15, 118, 110, 0.4);
  cursor: pointer; z-index: 50;
  font-family: inherit;
}
.fab:active { transform: scale(0.96); }

@media (max-width: 767px) {
  body { font-size: 14px; }

  .desktop-only { display: none !important; }
  .mobile-only { display: flex !important; }
  .fab.mobile-only { display: flex !important; align-items: center; justify-content: center; }
  #ordersCards.mobile-only { display: flex !important; flex-direction: column; }

  .topbar { padding: 12px 14px; }
  .logo { font-size: 16px; }
  .topbar-right { gap: 6px; }
  .user-name { font-size: 12px; max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .user-name .admin-tag { font-size: 10px; padding: 1px 5px; margin-left: 4px; }

  .filterbar {
    padding: 10px 14px; gap: 10px;
    flex-direction: column; align-items: stretch;
  }
  .tabs { overflow-x: auto; }
  .tab { white-space: nowrap; padding: 6px 12px; }
  .filters {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
  }
  .filters .input {
    grid-column: 1 / -1;
    width: 100%;
  }
  .filters .select { width: auto; }
  /* 새 주문 버튼은 FAB로 빼고 필터바에선 숨김 */
  #newOrderBtn { display: none; }
  .export-wrap { justify-self: end; }

  .table-wrap { padding: 0 12px 80px; }
  .footnote { font-size: 11px; }

  /* 모달: 풀스크린에 가깝게 */
  .modal { padding: 0; align-items: stretch; }
  .modal-box {
    width: 100%; height: 100%; max-height: 100%;
    border-radius: 0;
  }
  .modal-head { padding: 14px 16px; }
  .modal-head h2 { font-size: 15px; }
  .modal-body { padding: 16px; }
  .modal-foot { padding: 12px 16px; }

  .grid { grid-template-columns: 1fr; gap: 12px; }
  .grid label.span2 { grid-column: span 1; }
  .grid input, .grid select, .grid textarea { font-size: 16px; padding: 10px 12px; } /* iOS 자동 확대 방지: 16px+ */

  .comments-list { max-height: 200px; }
  .comments-input input { font-size: 16px; height: 36px; }
  .comments-input .btn { height: 36px; }

  /* 멤버 관리 */
  .member-row {
    grid-template-columns: 1fr;
    gap: 6px; padding: 10px 12px;
  }
  .member-row .email { font-size: 12px; }
  .member-row input, .member-row select { height: 36px; font-size: 14px; }
  .member-row .btn { height: 32px; }
  .member-add-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }
  .member-add-row input { font-size: 16px; height: 36px; }

  .export-menu { right: 0; min-width: 160px; }
  .toast { bottom: 90px; font-size: 13px; max-width: 90%; text-align: center; }
}

/* 태블릿 (768~1024): 테이블은 보이되 좀 좁게 */
@media (min-width: 768px) and (max-width: 1024px) {
  .table-wrap { padding: 0 12px 40px; overflow-x: auto; }
  table.orders { min-width: 1100px; }
  .filterbar { padding: 12px 14px; }
  .input { width: 200px; }
}
