/* Maclife Avatar — palette: deep blue #0066ab, medium #39a4dc, cyan #12c3f4 */
:root{
  --th-green:#0066ab;       /* primary (giữ tên cho backward compat) */
  --th-green-dark:#004f87;  /* primary dark */
  --th-lime:#39a4dc;        /* secondary medium blue */
  --th-blue:#12c3f4;        /* accent bright cyan */
  --th-red:#ed1c24;
  --th-text:#1f2937;
  --th-muted:#6b7280;
  --th-bg:#f3f9fd;          /* light blue tint background */
  --th-card:#ffffff;
  --th-border:#e5e7eb;
}

@media (prefers-color-scheme: dark){
  :root{
    --th-text:#e5e7eb;
    --th-muted:#9ca3af;
    --th-bg:#0b1220;
    --th-card:#111827;
    --th-border:rgba(255,255,255,.08);
  }
}

*{box-sizing:border-box}
html,body{margin:0;padding:0}
body{
  font-family:system-ui,-apple-system,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif;
  background:var(--th-bg);
  color:var(--th-text);
  -webkit-font-smoothing:antialiased;
  overflow-x:hidden;
}

.th-bg-hero{
  background:
    radial-gradient(circle at 0% 0%, rgba(0,102,171,.08), transparent 50%),
    radial-gradient(circle at 100% 0%, rgba(18,195,244,.08), transparent 50%),
    radial-gradient(circle at 50% 100%, rgba(57,164,220,.07), transparent 50%),
    var(--th-bg);
}

.th-card{
  background:var(--th-card);
  border:1px solid var(--th-border);
  border-radius:1.25rem;
  box-shadow:0 4px 24px rgba(0,0,0,.04), 0 1px 2px rgba(0,0,0,.03);
}

.th-btn{
  display:inline-flex;align-items:center;justify-content:center;gap:.5rem;
  padding:.75rem 1.25rem;border-radius:.75rem;
  font-weight:600;font-size:.95rem;
  border:1px solid var(--th-border);
  background:var(--th-card);color:var(--th-text);
  cursor:pointer;transition:all .15s ease;
  text-decoration:none;
}
.th-btn:hover{transform:translateY(-1px);box-shadow:0 4px 12px rgba(0,0,0,.08)}
.th-btn[disabled]{opacity:.5;cursor:not-allowed;transform:none;box-shadow:none}
.th-btn.primary{
  background:linear-gradient(135deg,var(--th-green),var(--th-green-dark));
  color:#fff;border-color:transparent;
  box-shadow:0 4px 14px rgba(0,102,171,.28);
}
.th-btn.primary:hover{box-shadow:0 6px 20px rgba(0,102,171,.4)}
.th-btn.accent{
  background:linear-gradient(135deg,var(--th-blue),var(--th-lime));
  color:#fff;border-color:transparent;
  box-shadow:0 4px 14px rgba(18,195,244,.25);
}
.th-btn.ghost{background:transparent}
.th-btn.sm{padding:.4rem .75rem;font-size:.85rem;border-radius:.5rem}

.th-input{
  width:100%;
  padding:.625rem .875rem;
  border:1px solid var(--th-border);
  border-radius:.625rem;
  background:var(--th-card);
  color:var(--th-text);
  outline:none;
  transition:border .15s, box-shadow .15s;
  font-size:.95rem;
}
.th-input:focus{
  border-color:var(--th-green);
  box-shadow:0 0 0 3px rgba(0,102,171,.18);
}

.th-slider{
  -webkit-appearance:none;appearance:none;
  width:100%;height:6px;border-radius:6px;
  background:linear-gradient(to right, var(--th-green), var(--th-blue));
  outline:none;cursor:pointer;
}
.th-slider::-webkit-slider-thumb{
  -webkit-appearance:none;appearance:none;
  width:22px;height:22px;border-radius:50%;
  background:#fff;border:3px solid var(--th-green);
  cursor:pointer;box-shadow:0 2px 8px rgba(0,102,171,.25);
}
.th-slider::-moz-range-thumb{
  width:22px;height:22px;border-radius:50%;
  background:#fff;border:3px solid var(--th-green);
  cursor:pointer;
}
.th-slider:disabled{opacity:.4}

.th-canvas{
  display:block;width:100%;height:auto;
  background:
    linear-gradient(45deg,#f3f4f6 25%, transparent 25%),
    linear-gradient(-45deg,#f3f4f6 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #f3f4f6 75%),
    linear-gradient(-45deg, transparent 75%, #f3f4f6 75%);
  background-size:20px 20px;
  background-position:0 0, 0 10px, 10px -10px, -10px 0;
  background-color:#fff;
  border-radius:.875rem;
  touch-action:none;
  user-select:none;
  -webkit-user-select:none;
}
@media (prefers-color-scheme: dark){
  .th-canvas{
    background:
      linear-gradient(45deg,#1a1a1a 25%, transparent 25%),
      linear-gradient(-45deg,#1a1a1a 25%, transparent 25%),
      linear-gradient(45deg, transparent 75%, #1a1a1a 75%),
      linear-gradient(-45deg, transparent 75%, #1a1a1a 75%);
    background-size:20px 20px;
    background-position:0 0, 0 10px, 10px -10px, -10px 0;
    background-color:#0f172a;
  }
}

.th-chip{
  display:inline-flex;align-items:center;gap:.35rem;
  padding:.2rem .65rem;border-radius:999px;
  font-size:.75rem;font-weight:600;
}
.th-chip.green{background:rgba(0,102,171,.12);color:var(--th-green)}
.th-chip.blue{background:rgba(18,195,244,.15);color:#0a8dba}
.th-chip.red{background:rgba(237,28,36,.1);color:var(--th-red)}
.th-chip.gray{background:rgba(107,114,128,.12);color:var(--th-muted)}

.th-toast{
  position:fixed;left:50%;bottom:24px;transform:translateX(-50%) translateY(80px);
  background:#111827;color:#fff;padding:.75rem 1.25rem;border-radius:.75rem;
  font-size:.9rem;font-weight:500;
  box-shadow:0 8px 30px rgba(0,0,0,.2);
  opacity:0;transition:transform .3s, opacity .3s;
  z-index:1000;max-width:90vw;
}
.th-toast.show{transform:translateX(-50%) translateY(0);opacity:1}
.th-toast.success{background:var(--th-green)}
.th-toast.error{background:var(--th-red)}

.th-hidden{display:none !important}

.th-skeleton{
  background:linear-gradient(90deg, var(--th-border) 0%, rgba(255,255,255,.5) 50%, var(--th-border) 100%);
  background-size:200% 100%;
  animation:th-skel 1.4s ease-in-out infinite;
  border-radius:.5rem;
}
@keyframes th-skel{
  0%{background-position:200% 0}
  100%{background-position:-200% 0}
}

.th-fade-in{animation:th-fade .35s ease-out}
@keyframes th-fade{
  from{opacity:0;transform:translateY(8px)}
  to{opacity:1;transform:translateY(0)}
}

.th-pulse{animation:th-pulse 2s ease-in-out infinite}
@keyframes th-pulse{
  0%,100%{opacity:1}
  50%{opacity:.6}
}

/* In-app banner */
.th-inapp-banner{
  background:linear-gradient(135deg, #fff7e6, #fffae6);
  border:1px solid #fbbf24;
  color:#92400e;
  padding:.75rem 1rem;border-radius:.75rem;font-size:.85rem;
  display:flex;align-items:flex-start;gap:.6rem;
}

/* Admin sidebar */
.th-admin-sidebar a{
  display:flex;align-items:center;gap:.6rem;
  padding:.6rem .9rem;border-radius:.5rem;
  color:#e5e7eb;text-decoration:none;font-size:.92rem;
  transition:background .15s;
}
.th-admin-sidebar a:hover{background:rgba(255,255,255,.08)}
.th-admin-sidebar a.active{background:rgba(0,166,81,.2);color:#fff}

/* Result panel */
.th-result{
  background:var(--th-card);
  border:2px dashed var(--th-green);
  border-radius:1rem;
  padding:1rem;
  text-align:center;
}
.th-result img{
  max-width:100%;border-radius:.75rem;
  box-shadow:0 8px 30px rgba(0,0,0,.12);
}

/* Hide scrollbar in main editor area on mobile */
@media (max-width:640px){
  .th-controls-grid{grid-template-columns:1fr !important}
}

/* ===== In-app browser warning modal ===== */
.th-modal-overlay{
  position:fixed;inset:0;z-index:9999;
  background:rgba(15,23,42,.55);
  backdrop-filter:blur(6px);
  -webkit-backdrop-filter:blur(6px);
  display:flex;align-items:center;justify-content:center;
  padding:1rem;
  animation:th-overlay-in .25s ease-out;
}
@keyframes th-overlay-in{from{opacity:0}to{opacity:1}}

.th-modal-card{
  position:relative;
  background:#fff;color:#1f2937;
  width:100%;max-width:420px;
  border-radius:1.5rem;
  padding:1.75rem 1.5rem 1.25rem;
  box-shadow:0 25px 50px -12px rgba(0,0,0,.4);
  animation:th-card-in .35s cubic-bezier(.16,1,.3,1);
  text-align:center;
  max-height:90vh;overflow-y:auto;
}
@keyframes th-card-in{from{opacity:0;transform:translateY(20px) scale(.96)}to{opacity:1;transform:translateY(0) scale(1)}}

.th-modal-close{
  position:absolute;top:.75rem;right:1rem;
  background:none;border:0;cursor:pointer;
  font-size:1.75rem;line-height:1;color:#94a3b8;
  width:32px;height:32px;border-radius:50%;
  display:flex;align-items:center;justify-content:center;
  transition:background .15s;
}
.th-modal-close:hover{background:#f1f5f9;color:#475569}

.th-modal-icon{
  font-size:3rem;line-height:1;margin-bottom:.5rem;
  animation:th-bounce 2s ease-in-out infinite;
}
@keyframes th-bounce{
  0%,100%{transform:translateY(0)}
  50%{transform:translateY(-6px)}
}

.th-modal-title{
  font-size:1.15rem;font-weight:700;
  margin:.5rem 0 .5rem;line-height:1.3;
  color:#0f172a;
}

.th-modal-sub{
  font-size:.875rem;color:#64748b;line-height:1.5;
  margin:0 0 1rem;
}

.th-modal-howto{
  background:linear-gradient(135deg,#eff6ff,#f0f9ff);
  border:1px solid #bfdbfe;
  border-radius:.875rem;
  padding:.875rem 1rem;
  text-align:left;
  margin-bottom:1rem;
}
.th-modal-howto-title{
  font-weight:600;font-size:.85rem;
  color:#0066ab;margin-bottom:.4rem;
}
.th-modal-howto ol{
  margin:0;padding-left:1.2rem;
  font-size:.85rem;color:#1f2937;line-height:1.7;
}
.th-modal-howto ol li{margin-bottom:.15rem}

.th-modal-actions{
  display:flex;gap:.5rem;flex-direction:column;
  margin-bottom:.5rem;
}
.th-modal-actions .th-btn{width:100%;justify-content:center;padding:.85rem 1rem}

.th-modal-continue{
  display:block;width:100%;
  background:none;border:0;
  padding:.6rem;margin-top:.25rem;
  color:#94a3b8;font-size:.85rem;
  cursor:pointer;border-radius:.5rem;
  transition:color .15s;
}
.th-modal-continue:hover{color:#475569}

.th-modal-url{
  font-size:.7rem;color:#cbd5e1;
  word-break:break-all;
  margin-top:.5rem;
  padding:.4rem;
  background:#f8fafc;border-radius:.4rem;
  font-family:ui-monospace,SFMono-Regular,Menlo,monospace;
  display:none;
}
.th-modal-url.show{display:block}

/* ===== Desktop: fit toàn bộ giao diện trong viewport ===== */
@media (min-width: 1024px) {
  html, body { min-height: 100%; }
  body { display: flex; flex-direction: column; overflow-y: auto; }
  body > header { flex-shrink: 0; }
  body > footer { flex-shrink: 0; padding: .5rem; }
  .th-main {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    width: 100%;
  }
  .th-hero { flex-shrink: 0; }
  #th-inapp-hint { flex-shrink: 0; }

  .th-editor-grid {
    flex: 1;
    min-height: 0;
    /* Cột trái auto-size theo canvas, cột phải 340px, cả cụm căn giữa */
    grid-template-columns: auto 340px !important;
    justify-content: center !important;
  }
  .th-canvas-card {
    min-height: 0;
    overflow: hidden;
    /* Card co lại bằng kích thước wrap (đã set aspect-ratio đúng tỉ lệ frame) */
    width: fit-content !important;
    max-width: 100% !important;
    justify-self: center;
    /* Quan trọng: không cho grid stretch chiều cao, dùng natural height = wrap + text + padding */
    align-self: start !important;
    height: fit-content !important;
  }
  /* Wrap có aspect-ratio = tỉ lệ thật của frame (inline style từ PHP),
     tự fit max-h (viewport - header/footer/hero/padding/text) và max-w (cột grid còn lại) */
  .th-canvas-wrap {
    min-height: 0;
    overflow: hidden;
    /* aspect-ratio đặt qua inline style */
    /* Trừ: header ~70, hero ~80, footer ~30, main padding ~32, card padding ~32, text dưới ~30 = ~274 → 290 cho an toàn */
    max-height: calc(100vh - 290px);
    max-width: 100%;
    width: auto;
    height: auto;
  }
  .th-canvas-wrap > canvas#th-canvas,
  .th-canvas-wrap > #th-skel {
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
    max-height: none !important;
  }
  .th-controls-card {
    overflow-y: auto;
    max-height: 100%;
  }

  /* Result panel khi xuất hiện sẽ scroll body — không cần thay đổi nữa vì body luôn auto-scroll */
}

/* Ultra-wide screens — không quá rộng */
@media (min-width: 1280px) {
  .th-editor-grid { grid-template-columns: auto 360px !important; }
}
