/* ============================================================
   TextTrace — Image Text Detection & Replacement Editor
   Professional minimal light theme
   ============================================================ */

:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --border: #e2e5ea;
  --border-strong: #cdd2da;
  --text: #1b1f26;
  --text-2: #5b6472;
  --text-3: #8b93a1;
  --accent: #2563eb;
  --accent-soft: #eaf1fe;
  --accent-strong: #1d4ed8;
  --green: #16a34a;
  --green-soft: #e9f9ef;
  --amber: #d97706;
  --amber-soft: #fdf3e3;
  --red: #dc2626;
  --red-soft: #fdecec;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 4px 16px rgba(16, 24, 40, .06);
  --shadow-lg: 0 8px 40px rgba(16, 24, 40, .18);
  --radius: 8px;
  --topbar-h: 52px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "SF Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
}

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

html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------------- Top bar ---------------- */
.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  gap: 8px;
  z-index: 50;
}
.topbar-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { display: flex; align-items: center; gap: 8px; color: var(--accent-strong); font-weight: 700; font-size: 15px; }
.logo-text { color: var(--text); letter-spacing: -.01em; }
.logo-sub { color: var(--text-3); font-size: 11px; border-left: 1px solid var(--border); padding-left: 10px; white-space: nowrap; }

.topbar-actions { display: flex; align-items: center; gap: 6px; }
.tb-divider { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 13px;
  font: 600 12.5px var(--font);
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s, box-shadow .12s;
}
.btn:hover:not(:disabled) { background: var(--panel-2); border-color: #b8bfca; }
.btn:active:not(:disabled) { transform: translateY(.5px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { color: var(--red); border-color: #f0c4c4; }
.btn.danger:hover:not(:disabled) { background: var(--red-soft); border-color: var(--red); }
.btn.small { height: 28px; padding: 0 10px; font-size: 12px; border-radius: 6px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  font-size: 15px;
  color: var(--text-2);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .12s, color .12s;
}
.icon-btn:hover:not(:disabled) { background: var(--panel-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.icon-btn.active { background: var(--accent-soft); color: var(--accent-strong); border-color: #c7d9fb; }

.mini-btn {
  float: right;
  height: 20px;
  padding: 0 8px;
  font: 600 10.5px var(--font);
  color: var(--accent-strong);
  background: var(--accent-soft);
  border: 1px solid #c7d9fb;
  border-radius: 5px;
  cursor: pointer;
}
.mini-btn:hover { background: #dce8fd; }

/* ---------------- Workspace layout ---------------- */
#workspace {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 272px 1fr 304px;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.panel {
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
}
#left-panel { border-right: 1px solid var(--border); }
#right-panel { border-left: 1px solid var(--border); }

.panel-section { padding: 12px 14px; border-bottom: 1px solid var(--border); }
.panel-section.grow { flex: 1; display: flex; flex-direction: column; min-height: 140px; }
.panel-section h3 { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--text-3); margin-bottom: 10px; display: flex; align-items: center; gap: 6px; }
.subhead { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin: 12px 0 8px; display: flex; align-items: center; gap: 6px; }
.subhead:first-child { margin-top: 0; }

.badge {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 10px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 99px;
}

.hint { color: var(--text-3); font-size: 11.5px; line-height: 1.55; }
.hint-inline { color: var(--text-3); font-weight: 400; font-size: 10.5px; text-transform: none; letter-spacing: 0; }

/* ---------------- Image section ---------------- */
.empty-image { text-align: center; padding: 18px 8px; color: var(--text-2); }
.empty-icon { color: var(--text-3); margin-bottom: 10px; }
.empty-image p { margin-bottom: 12px; line-height: 1.5; }
.empty-image .btn { margin-bottom: 10px; }

.img-meta { margin-bottom: 10px; }
.img-name { font-weight: 600; font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.img-dims { color: var(--text-3); font-size: 11px; margin-top: 2px; font-family: var(--mono); }

.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.btn-row .btn { flex: 1; justify-content: center; min-width: 70px; }

/* ---------------- View section ---------------- */
.zoom-row { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }
.zoom-level { flex: 1; text-align: center; font: 600 11.5px var(--mono); color: var(--text-2); background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 4px 0; }

.check { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--text-2); margin: 7px 0; cursor: pointer; user-select: none; }
.check input { accent-color: var(--accent); width: 14px; height: 14px; cursor: pointer; }

.seg { display: flex; background: var(--panel-2); border: 1px solid var(--border); border-radius: 7px; padding: 2px; gap: 2px; margin: 8px 0; }
.seg button {
  flex: 1;
  height: 26px;
  font: 600 11.5px var(--font);
  color: var(--text-2);
  background: transparent;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
}
.seg button:hover { color: var(--text); }
.seg button.active { background: var(--panel); color: var(--accent-strong); box-shadow: 0 1px 2px rgba(16,24,40,.12); }

.slider-label { display: block; font-size: 11.5px; color: var(--text-2); margin: 8px 0; }
.slider-label input[type="range"] { width: 100%; margin-top: 5px; accent-color: var(--accent); cursor: pointer; }
.slider-val { font: 600 10.5px var(--mono); color: var(--text-3); }

/* ---------------- Detected text list ---------------- */
#list-search {
  width: 100%;
  height: 30px;
  padding: 0 10px;
  font: 400 12px var(--font);
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  outline: none;
  margin-bottom: 8px;
}
#list-search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.text-list { list-style: none; overflow-y: auto; flex: 1; min-height: 60px; }
.text-list li {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 2px;
  transition: background .1s;
}
.text-list li:hover { background: var(--panel-2); }
.text-list li.selected { background: var(--accent-soft); border-color: #c7d9fb; }
.text-list li .num {
  font: 700 10px var(--mono);
  color: var(--text-3);
  min-width: 18px;
  text-align: right;
}
.text-list li.selected .num { color: var(--accent-strong); }
.text-list li .txt {
  flex: 1;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.text-list li .txt .replaced { color: var(--green); font-weight: 600; }
.text-list li .conf { font: 600 9.5px var(--mono); color: var(--text-3); }
.text-list li .conf.low { color: var(--amber); }
.text-list li .li-actions { display: none; gap: 2px; }
.text-list li:hover .li-actions, .text-list li.selected .li-actions { display: flex; }
.li-actions button {
  width: 20px; height: 20px;
  font-size: 11px;
  color: var(--text-3);
  background: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.li-actions button:hover { background: #e8ebf0; color: var(--text); }
.li-actions button.mark-hidden { color: var(--text-3); opacity: .6; }

.list-empty { color: var(--text-3); font-size: 11.5px; text-align: center; padding: 22px 10px; line-height: 1.6; }

/* ---------------- Center / canvas ---------------- */
#center-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#canvas-wrap {
  position: relative;
  flex: 1;
  overflow: hidden;
  background:
    linear-gradient(45deg, #e8eaee 25%, transparent 25%, transparent 75%, #e8eaee 75%),
    linear-gradient(45deg, #e8eaee 25%, #eef0f3 25%, #eef0f3 75%, #e8eaee 75%);
  background-size: 24px 24px;
  background-position: 0 0, 12px 12px;
}
#view-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: default; }
#view-canvas.panning { cursor: grabbing; }
#view-canvas.moving { cursor: move; }
#view-canvas.brushing { cursor: none; }
#view-canvas.resizing { cursor: nwse-resize; }

#canvas-status-bar {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 12px;
  background: rgba(255,255,255,.85);
  backdrop-filter: blur(6px);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-2);
  pointer-events: none;
}
#status-coords { font-family: var(--mono); color: var(--text-3); }

/* scan overlay */
#scan-overlay {
  position: absolute;
  inset: 0;
  background: rgba(24, 28, 36, .55);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
.scan-box {
  background: var(--panel);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 26px 30px;
  width: 320px;
  text-align: center;
}
.spinner {
  width: 34px; height: 34px;
  margin: 0 auto 14px;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.scan-status { font-weight: 600; font-size: 13px; margin-bottom: 12px; min-height: 18px; }
.progress { height: 6px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 99px; overflow: hidden; }
#scan-progress { height: 100%; width: 0%; background: var(--accent); border-radius: 99px; transition: width .2s; }
.scan-percent { font: 600 11px var(--mono); color: var(--text-3); margin: 8px 0 14px; }

/* compare divider */
#compare-divider {
  position: absolute;
  top: 0; bottom: 0;
  width: 2px;
  background: var(--accent);
  z-index: 15;
  cursor: ew-resize;
  box-shadow: 0 0 0 1px rgba(255,255,255,.5);
}
.divider-handle {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 26px; height: 26px;
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  box-shadow: var(--shadow);
}

/* ---------------- Right panel / editor ---------------- */
.editor-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 0; }
.editor-head h3 { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--text-3); display: flex; align-items: center; gap: 6px; }
#sel-num { background: var(--accent); color: #fff; }

.tip-box { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 11.5px; color: var(--text-2); line-height: 1.5; margin-top: 12px; }

.stat { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-2); padding: 4px 0; }
.stat b { font-family: var(--mono); font-size: 11.5px; color: var(--text); }

.orig-text {
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  padding: 7px 9px;
  max-height: 92px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
}

.field-label { display: block; font-size: 11.5px; color: var(--text-2); margin: 9px 0 5px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 11.5px; color: var(--text-2); }
.field input[type="number"], .field input[type="text"] {
  height: 28px;
  padding: 0 8px;
  font: 500 12px var(--mono);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  outline: none;
  width: 100%;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.field input[type="color"] { width: 100%; height: 28px; padding: 2px; border: 1px solid var(--border-strong); border-radius: 6px; background: var(--panel); cursor: pointer; }
.field input:disabled { opacity: .4; cursor: not-allowed; }
.field select {
  height: 28px;
  font: 500 12px var(--font);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--panel);
  padding: 0 6px;
  outline: none;
  width: 100%;
}
.field select:focus { border-color: var(--accent); }

select#f-family, select#set-lang, select#set-maxdim, select#set-export {
  width: 100%;
  height: 30px;
  font: 500 12px var(--font);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--panel);
  padding: 0 6px;
  outline: none;
}
select:focus { border-color: var(--accent); }

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

#orig-text {
  font: 500 12.5px var(--mono);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--text-2);
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 34px;
}

#replace-text {
  width: 100%;
  font: 500 12.5px var(--mono);
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 8px 10px;
  resize: vertical;
  outline: none;
  line-height: 1.45;
}
#replace-text:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.warning {
  margin-top: 8px;
  font-size: 11.5px;
  color: #92400e;
  background: var(--amber-soft);
  border: 1px solid #f2d9ad;
  border-radius: 6px;
  padding: 7px 10px;
  line-height: 1.45;
}

#preview-canvas {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background:
    linear-gradient(45deg, #eceef1 25%, transparent 25%, transparent 75%, #eceef1 75%),
    linear-gradient(45deg, #eceef1 25%, #f6f7f9 25%, #f6f7f9 75%, #eceef1 75%);
  background-size: 12px 12px;
  background-position: 0 0, 6px 6px;
}

/* ---------------- Footer ---------------- */
.footer {
  height: 26px;
  min-height: 26px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  font-size: 10.5px;
  color: var(--text-3);
}
.footer-sep { opacity: .5; }

/* ---------------- Modals ---------------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 32, .5);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-card {
  background: var(--panel);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 420px;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  animation: pop .16s ease-out;
}
@keyframes pop { from { transform: scale(.96) translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px 0; }
.modal-head h3 { font-size: 15px; font-weight: 700; }
.modal-body { padding: 14px 18px 18px; overflow-y: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.export-meta {
  font: 500 11.5px var(--mono);
  color: var(--text-2);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 9px 12px;
  margin-bottom: 12px;
  line-height: 1.6;
}
.modal-body input[type="range"] { width: 100%; accent-color: var(--accent); }

/* ---------------- Drop overlay ---------------- */
#drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(37, 99, 235, .12);
  border: 3px dashed var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.drop-box {
  background: var(--panel);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 22px 40px;
  font-size: 16px;
  font-weight: 700;
  color: var(--accent-strong);
}

/* ---------------- Toasts ---------------- */
#toasts {
  position: fixed;
  bottom: 38px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 300;
  pointer-events: none;
}
.toast {
  background: #21262f;
  color: #f4f6f8;
  font-size: 12.5px;
  font-weight: 500;
  padding: 10px 18px;
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  max-width: 78vw;
  animation: toast-in .18s ease-out;
  line-height: 1.45;
}
.toast.error { background: #7f1d1d; }
.toast.success { background: #14532d; }
.toast.out { opacity: 0; transform: translateY(8px); transition: all .25s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ---------------- Scrollbars ---------------- */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #c8cdd5; border-radius: 99px; border: 2px solid var(--panel); }
::-webkit-scrollbar-thumb:hover { background: #aeb5c0; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------------- Mobile / responsive ---------------- */
.mobile-only { display: none; }

@media (max-width: 1180px) {
  #workspace { grid-template-columns: 240px 1fr 272px; }
  .logo-sub { display: none; }
}

@media (max-width: 920px) {
  #workspace { grid-template-columns: 1fr; }
  .mobile-only { display: inline-flex; }
  .btn-label { display: none; }
  .btn { padding: 0 10px; }

  #left-panel, #right-panel {
    position: fixed;
    top: var(--topbar-h);
    bottom: 26px;
    width: min(320px, 86vw);
    z-index: 60;
    box-shadow: var(--shadow-lg);
    transform: translateX(-105%);
    transition: transform .22s ease;
  }
  #right-panel {
    left: auto;
    right: 0;
    transform: translateX(105%);
  }
  #left-panel.open { transform: none; }
  #right-panel.open { transform: none; }
  #panel-backdrop {
    position: fixed;
    inset: var(--topbar-h) 0 26px 0;
    background: rgba(20, 24, 32, .35);
    z-index: 55;
  }
}

@media (max-width: 560px) {
  .topbar { padding: 0 8px; gap: 4px; }
  .topbar-actions { gap: 3px; }
  .tb-divider { margin: 0 1px; }
  .footer span:last-child { display: none; }
}
