/* ================================================================
   DocAnnotate Pro – Main Stylesheet  v2
   Light/Dark mode · Responsive Desktop · Tablet · Mobile
   ================================================================ */

/* ── CSS Variables ─────────────────────────────────────────────── */
:root {
  --primary:        #4f46e5;
  --primary-light:  #6366f1;
  --primary-dark:   #3730a3;
  --accent:         #06b6d4;
  --danger:         #ef4444;
  --success:        #22c55e;
  --warning:        #f59e0b;

  /* Light theme */
  --bg:             #f1f5f9;
  --surface:        #ffffff;
  --surface2:       #f8fafc;
  --border:         #e2e8f0;
  --text:           #1e293b;
  --text-muted:     #64748b;
  --shadow:         rgba(0,0,0,.08);
  --shadow-md:      rgba(0,0,0,.14);

  --header-h:       56px;
  --sidebar-w:      238px;
  --radius:         8px;
  --radius-lg:      12px;
  --transition:     .2s ease;
}

[data-theme="dark"] {
  --bg:          #0f172a;
  --surface:     #1e293b;
  --surface2:    #162032;
  --border:      #334155;
  --text:        #f1f5f9;
  --text-muted:  #94a3b8;
  --shadow:      rgba(0,0,0,.3);
  --shadow-md:   rgba(0,0,0,.45);
}

/* ── Reset & Base ───────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: 'Inter', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

input, select, textarea, button { font-family: inherit; }
textarea { user-select: text; }

/* ── App Shell ──────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* ═══════════════════════════════════════════════════════════════════
   HEADER
═══════════════════════════════════════════════════════════════════ */
#app-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px;
  box-shadow: 0 1px 6px var(--shadow);
  z-index: 100;
  flex-shrink: 0;
}

.header-left  { display: flex; align-items: center; gap: 8px; min-width: 160px; }
.header-center { flex: 1; text-align: center; overflow: hidden; }
.header-right  { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

.logo {
  display: flex; align-items: center; gap: 7px;
  font-size: 1.05rem; font-weight: 700;
  color: var(--primary); white-space: nowrap;
}
.logo i   { font-size: 1.2rem; }
.logo em  { color: var(--accent); font-style: normal; }

#doc-filename {
  font-size: .82rem; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 280px; display: inline-block;
}

/* Buttons */
.icon-btn {
  width: 36px; height: 36px;
  border: none; background: transparent;
  color: var(--text-muted); border-radius: var(--radius);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  font-size: 1rem; transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--border); color: var(--primary); }

/* Lock button – special state colors */
.lock-btn { position: relative; }
.lock-btn.locked { color: var(--primary); background: rgba(79,70,229,.12); }
.lock-btn.locked:hover { background: rgba(79,70,229,.2); }

.btn {
  padding: 7px 13px; border-radius: var(--radius); border: none;
  cursor: pointer; font-size: .8rem; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px;
  transition: all var(--transition); white-space: nowrap;
}
.btn-primary  { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline  { background: transparent; border: 1.5px solid var(--border); color: var(--text); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-danger   { background: var(--danger); color: #fff; }
.btn-danger:hover { opacity: .88; }

.full-btn { width: 100%; justify-content: center; padding: 9px; font-size: .82rem; }
.w50 { flex: 1; justify-content: center; }

/* Page nav / zoom */
.page-nav { display: flex; align-items: center; gap: 3px; }
.page-nav span { font-size: .8rem; min-width: 48px; text-align: center; color: var(--text-muted); }
.zoom-controls { display: flex; align-items: center; gap: 3px; }
#zoomLevel { font-size: .8rem; min-width: 42px; text-align: center; color: var(--text-muted); }
.hdr-undoredo { display: flex; gap: 2px; }

/* ═══════════════════════════════════════════════════════════════════
   MAIN AREA
═══════════════════════════════════════════════════════════════════ */
#main-area {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ═══════════════════════════════════════════════════════════════════
   SIDEBAR
═══════════════════════════════════════════════════════════════════ */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  transition: width var(--transition), min-width var(--transition);
  z-index: 50;
  flex-shrink: 0;
}
#sidebar.closed { width: 0; min-width: 0; border-right: none; }
#sidebar::-webkit-scrollbar { width: 4px; }
#sidebar::-webkit-scrollbar-track { background: transparent; }
#sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

.sidebar-section {
  padding: 10px 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

/* Push export section to bottom */
.export-section { margin-top: auto; }

.section-label {
  font-size: .66rem; font-weight: 700; letter-spacing: .08em;
  color: var(--text-muted); text-transform: uppercase;
  margin-bottom: 8px;
  display: flex; align-items: center; justify-content: space-between;
}

/* Tool Grid */
.tool-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}
.tool-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; padding: 7px 2px;
  border: 1.5px solid var(--border); border-radius: var(--radius);
  background: var(--surface2); color: var(--text-muted);
  cursor: pointer; font-size: .6rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em;
  transition: all var(--transition);
}
.tool-btn i    { font-size: 1rem; }
.tool-btn:hover { border-color: var(--primary-light); color: var(--primary); background: var(--surface); }
.tool-btn.active { border-color: var(--primary); background: var(--primary); color: #fff;
                   box-shadow: 0 2px 8px rgba(79,70,229,.35); }

/* Option rows */
.option-row { margin-bottom: 9px; }
.option-row > label {
  display: block; font-size: .7rem; font-weight: 600;
  color: var(--text-muted); margin-bottom: 4px;
}
.option-row select {
  width: 100%; padding: 6px 8px;
  border: 1.5px solid var(--border); border-radius: var(--radius);
  background: var(--surface2); color: var(--text);
  font-size: .78rem; outline: none; cursor: pointer;
}
.option-row select:focus { border-color: var(--primary); }

/* Full-width range input */
.full-range {
  width: 100%; margin-top: 2px;
  accent-color: var(--primary); cursor: pointer;
  height: 4px;
}

/* Badge showing current value next to label */
.val-badge {
  display: inline-block; min-width: 22px; text-align: center;
  background: var(--primary); color: #fff;
  font-size: .62rem; font-weight: 700;
  border-radius: 4px; padding: 1px 4px;
  vertical-align: middle;
}

.color-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.color-row input[type=color] {
  width: 32px; height: 32px;
  border: 2px solid var(--border); border-radius: var(--radius);
  cursor: pointer; padding: 0; background: none;
}

.color-presets { display: flex; gap: 4px; flex-wrap: wrap; }
.color-dot {
  width: 18px; height: 18px; border-radius: 50%; cursor: pointer;
  border: 2px solid transparent;
  transition: border-color var(--transition), transform var(--transition);
}
.color-dot:hover, .color-dot.active { border-color: var(--text); transform: scale(1.2); }

.btn-group { display: flex; gap: 4px; }
.style-btn {
  padding: 5px 9px; border: 1.5px solid var(--border); border-radius: var(--radius);
  background: var(--surface2); color: var(--text); cursor: pointer; font-size: .82rem;
  transition: all var(--transition);
}
.style-btn:hover  { border-color: var(--primary); color: var(--primary); }
.style-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Toggle pill (fill enable) */
.toggle-label { display: flex; align-items: center; gap: 5px; cursor: pointer; }
.toggle-label input { display: none; }
.toggle-pill {
  width: 30px; height: 16px;
  background: var(--border); border-radius: 8px;
  position: relative; transition: background var(--transition);
}
.toggle-pill::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 12px; height: 12px; border-radius: 50%;
  background: #fff; transition: left var(--transition);
}
.toggle-label input:checked ~ .toggle-pill { background: var(--primary); }
.toggle-label input:checked ~ .toggle-pill::after { left: 16px; }
.small { font-size: .72rem; color: var(--text-muted); }

/* Layer items */
.small-btn {
  width: 22px; height: 22px; border: 1px solid var(--border); border-radius: 5px;
  background: var(--surface2); color: var(--text-muted); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: .68rem; transition: all var(--transition);
}
.small-btn:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.layer-actions { display: flex; gap: 3px; }

#layersList { display: flex; flex-direction: column; gap: 3px; max-height: 140px; overflow-y: auto; }
.layer-item {
  padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px;
  font-size: .7rem; cursor: pointer;
  display: flex; align-items: center; gap: 5px;
  background: var(--surface2); color: var(--text-muted);
  transition: all var(--transition);
}
.layer-item:hover  { border-color: var(--primary-light); color: var(--text); }
.layer-item.selected { border-color: var(--primary); background: rgba(79,70,229,.1); color: var(--primary); }
.layer-item i { font-size: .85rem; }

/* Undo row */
.undo-row { display: flex; gap: 6px; }

/* Export column */
.export-col { display: flex; flex-direction: column; gap: 6px; }

/* ═══════════════════════════════════════════════════════════════════
   WORKSPACE
═══════════════════════════════════════════════════════════════════ */
#workspace {
  flex: 1; overflow: auto;
  background: var(--bg);
  display: flex; align-items: flex-start; justify-content: center;
  position: relative;
}
#workspace::-webkit-scrollbar { width: 8px; height: 8px; }
#workspace::-webkit-scrollbar-track { background: transparent; }
#workspace::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }
#workspace::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Locked workspace: disable scroll on touch */
#workspace.doc-locked {
  overflow: hidden;
  touch-action: none;
}

/* Upload prompt */
#uploadPrompt {
  display: flex; align-items: center; justify-content: center;
  width: 100%; min-height: 100%; padding: 32px;
}
.upload-card {
  background: var(--surface); border: 2px dashed var(--border);
  border-radius: var(--radius-lg); padding: 48px 40px;
  text-align: center; max-width: 420px; width: 100%;
  transition: border-color var(--transition);
}
.upload-card:hover { border-color: var(--primary); }
.upload-card > i  { font-size: 3.5rem; color: var(--primary); margin-bottom: 16px; display: block; }
.upload-card h2   { font-size: 1.4rem; margin-bottom: 8px; }
.upload-card p    { color: var(--text-muted); margin-bottom: 20px; font-size: .88rem; }
.upload-btns { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-bottom: 12px; }
.hint { font-size: .73rem; color: var(--text-muted); }

/* Page container */
#pageContainer {
  padding: 20px;
  display: flex; align-items: flex-start; justify-content: center;
  min-width: 100%; min-height: 100%;
}
#pageWrapper {
  position: relative;
  box-shadow: 0 4px 28px var(--shadow-md);
  border-radius: 4px;
  overflow: hidden;
  flex-shrink: 0;
}

/* Canvases */
#docCanvas, #annoCanvas {
  display: block; position: absolute; top: 0; left: 0;
  touch-action: none;
}
#docCanvas {
  z-index: 1;
  image-rendering: -webkit-optimize-contrast;
  image-rendering: auto;
}
#annoCanvas { z-index: 2; }

/* Object layer */
#objectLayer {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 3; pointer-events: none;
}
#objectLayer.interactive { pointer-events: all; }

/* Annotation objects */
.anno-object {
  position: absolute; cursor: default; user-select: none;
}
.anno-object.selected-obj {
  outline: 2px dashed var(--primary);
  outline-offset: 3px;
}
.anno-object.text-obj {
  padding: 4px 6px; border-radius: 4px; background: transparent;
  word-break: break-word; min-width: 40px; min-height: 24px;
  white-space: pre-wrap; line-height: 1.4;
}
.anno-object.text-obj.editing {
  outline: 2px solid var(--primary);
  background: rgba(255,255,255,.08); cursor: text;
}

/* Sticky note */
.anno-object.sticky-obj {
  border-radius: 6px;
  padding: 10px;
  box-shadow: 2px 3px 10px rgba(0,0,0,.18);
  min-width: 120px; min-height: 60px;
  word-break: break-word; white-space: pre-wrap;
  font-size: 13px; line-height: 1.5;
  color: #333;
}

/* Stamp */
.anno-object.stamp-obj {
  border: 3px solid currentColor;
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 1rem; font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
  opacity: .85;
  white-space: nowrap;
  background: transparent;
}

/* Resize handle */
.resize-handle {
  position: absolute; width: 11px; height: 11px;
  background: var(--primary); border: 2px solid #fff;
  border-radius: 2px; cursor: se-resize;
  bottom: -5px; right: -5px;
  box-shadow: 0 1px 4px rgba(0,0,0,.3);
}

/* SVG shapes */
.shape-obj svg { display: block; overflow: visible; }

/* Image objects */
.img-obj img { width: 100%; height: 100%; object-fit: fill; display: block; }

/* Cursor modes */
body[data-cursor="crosshair"] #annoCanvas { cursor: crosshair; }
body[data-cursor="text"]      #annoCanvas { cursor: text; }
body[data-cursor="pen"]       #annoCanvas { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Ccircle cx='8' cy='8' r='3' fill='%234f46e5'/%3E%3C/svg%3E") 8 8, crosshair; }
body[data-cursor="eraser"]    #annoCanvas { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Crect x='4' y='4' width='12' height='12' rx='2' fill='%23ef4444' opacity='.7'/%3E%3C/svg%3E") 10 10, cell; }
body[data-cursor="default"]   #annoCanvas { cursor: default; }

/* ═══════════════════════════════════════════════════════════════════
   MODALS
═══════════════════════════════════════════════════════════════════ */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 9999; display: flex !important;
  align-items: center; justify-content: center;
  padding: 16px; backdrop-filter: blur(4px);
  animation: fadeIn .15s ease;
}
.modal[style*="display:none"], .modal[style*="display: none"] { display: none !important; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-box {
  background: var(--surface); border-radius: var(--radius-lg);
  width: 100%; max-width: 480px;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
  animation: slideUp .2s ease; overflow: hidden;
}
.modal-box.small { max-width: 360px; }
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }

.modal-header {
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { font-size: .98rem; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.close-modal {
  width: 28px; height: 28px; border: none; background: none; cursor: pointer;
  border-radius: 50%; color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  transition: background var(--transition);
}
.close-modal:hover { background: var(--border); color: var(--text); }
.modal-body   { padding: 14px 18px; }
.modal-footer { padding: 10px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

.modal-body textarea {
  width: 100%; resize: vertical;
  border: 1.5px solid var(--border); border-radius: var(--radius);
  padding: 9px 10px; background: var(--surface2); color: var(--text);
  font-size: .88rem; outline: none;
  transition: border-color var(--transition); user-select: text;
}
.modal-body textarea:focus { border-color: var(--primary); }

.text-preview-wrap { margin-top: 10px; }
.text-preview-wrap label { font-size: .72rem; color: var(--text-muted); margin-bottom: 5px; display: block; }
#textPreview {
  padding: 9px 10px; border: 1.5px dashed var(--border); border-radius: var(--radius);
  background: var(--surface2); min-height: 38px;
  word-break: break-word; white-space: pre-wrap; line-height: 1.5;
}

/* Image upload area */
.img-upload-area {
  border: 2px dashed var(--border); border-radius: var(--radius-lg);
  padding: 28px; text-align: center; cursor: pointer;
  position: relative; transition: border-color var(--transition);
}
.img-upload-area:hover { border-color: var(--primary); }
.img-upload-area i  { font-size: 2.4rem; color: var(--primary); margin-bottom: 8px; display: block; }
.img-upload-area input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
#imgPreviewWrap { margin-top: 10px; text-align: center; }
#insertImgPreview { max-width: 100%; max-height: 200px; border-radius: var(--radius); border: 1px solid var(--border); }

/* Sticky note color chooser */
.sticky-colors { display: flex; gap: 8px; flex-wrap: wrap; }
.sticky-color-btn {
  width: 30px; height: 30px; border-radius: 6px;
  border: 2px solid transparent; cursor: pointer;
  box-shadow: 0 1px 4px rgba(0,0,0,.2);
  transition: border-color var(--transition), transform var(--transition);
}
.sticky-color-btn:hover, .sticky-color-btn.active { border-color: var(--primary); transform: scale(1.15); }

/* Stamp grid */
.stamp-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
}
.stamp-btn {
  padding: 12px 8px; border: 2px solid var(--border); border-radius: var(--radius);
  background: var(--surface2); color: var(--text); cursor: pointer;
  font-size: .82rem; font-weight: 700;
  transition: all var(--transition); text-align: center;
}
.stamp-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--surface); }

/* ═══════════════════════════════════════════════════════════════════
   TOAST
═══════════════════════════════════════════════════════════════════ */
#toastContainer {
  position: fixed; bottom: 18px; right: 18px; z-index: 99999;
  display: flex; flex-direction: column; gap: 7px; pointer-events: none;
}
.toast {
  padding: 9px 14px; border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font-size: .8rem; font-weight: 500;
  box-shadow: 0 4px 18px var(--shadow-md);
  border-left: 4px solid var(--primary);
  animation: toastIn .25s ease forwards; max-width: 280px;
  pointer-events: all;
}
.toast.success { border-color: var(--success); }
.toast.error   { border-color: var(--danger); }
.toast.warning { border-color: var(--warning); }
@keyframes toastIn  { from { transform: translateX(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes toastOut { to   { transform: translateX(40px); opacity: 0; } }

/* ═══════════════════════════════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
═══════════════════════════════════════════════════════════════════ */

/* Tablet ≤ 900px */
@media (max-width: 900px) {
  :root { --sidebar-w: 210px; }
  .logo span   { display: none; }
  #doc-filename { max-width: 140px; }
  .hdr-undoredo { display: none; }
}

/* Mobile ≤ 600px */
@media (max-width: 600px) {
  :root { --header-h: 50px; }
  #app-header { padding: 0 7px; gap: 5px; }

  /* Sidebar overlays on mobile */
  #sidebar {
    position: fixed; top: var(--header-h); left: 0; bottom: 0;
    z-index: 200; transform: translateX(0);
    transition: transform var(--transition);
    box-shadow: 4px 0 20px var(--shadow-md);
  }
  #sidebar.closed {
    transform: translateX(-100%);
    width: var(--sidebar-w); min-width: var(--sidebar-w);
    border-right: 1px solid var(--border);
  }

  .header-center { display: none; }
  .zoom-controls { display: none; }
  .page-nav      { display: none !important; }

  #pageContainer { padding: 8px; }
  .upload-card   { padding: 28px 16px; }
  .upload-btns   { flex-direction: column; align-items: center; }
}

/* Touch devices — bigger tap targets */
@media (hover: none) {
  .tool-btn  { padding: 9px 2px; }
  .icon-btn  { width: 40px; height: 40px; }
  .style-btn { padding: 7px 11px; }
}

/* Print */
@media print {
  #app-header, #sidebar, #toastContainer { display: none !important; }
  #workspace { overflow: visible; }
}
