/* === Base Reset === */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
}

/* === App Layout === */
.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  background: #f5f5f5;
  position: relative;
}

/* === Top Bar === */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  flex-shrink: 0;
  z-index: 100;
  min-height: 48px;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.site-logo {
  font-size: 16px;
  font-weight: 700;
  color: #ff4757;
  white-space: nowrap;
  text-decoration: none;
}

.usage-tip {
  font-size: 13px;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  flex: 1;
  min-width: 0;
}

/* === User Actions === */
.user-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 14px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
  touch-action: manipulation;
}

.btn-primary {
  background: #ff4757;
  color: #fff;
}

.btn-primary:hover,
.btn-primary:active {
  background: #ff3344;
}

.btn-secondary {
  background: #f1f2f6;
  color: #333;
}

.btn-secondary:hover,
.btn-secondary:active {
  background: #dfe4ea;
}

.btn-small {
  padding: 4px 10px;
  font-size: 12px;
}

.btn-link {
  background: none;
  color: #ff4757;
  text-decoration: none;
  padding: 4px 8px;
  font-size: 13px;
}

/* === Avatar in Top Bar === */
.avatar-mini {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid #ff4757;
  cursor: pointer;
  flex-shrink: 0;
}

.user-name {
  font-size: 13px;
  color: #333;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* === Main Content: Full-screen iframe === */
.main-content {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* === Toolbar for iframe URL === */
.url-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  flex-shrink: 0;
  position: relative;
}

.url-input-wrap {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
}

.url-input-wrap input {
  width: 100%;
  padding: 6px 28px 6px 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 13px;
  outline: none;
  min-width: 0;
}

.url-input-wrap input:focus {
  border-color: #ff4757;
}

.btn-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: #ccc;
  color: #fff;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.2s;
}

.btn-clear:hover {
  background: #ff4757;
}

.url-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  z-index: 200;
  display: none;
  max-height: 300px;
  overflow-y: auto;
}

.url-suggestions.active {
  display: block;
}

.url-suggestions-header {
  padding: 10px 14px;
  font-size: 12px;
  color: #888;
  border-bottom: 1px solid #f0f0f0;
}

.url-suggestion-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.2s;
  border-bottom: 1px solid #f5f5f5;
}

.url-suggestion-item:last-child {
  border-bottom: none;
}

.url-suggestion-item:hover {
  background: #f8f9fa;
}

.url-suggestion-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.url-suggestion-info {
  flex: 1;
  min-width: 0;
}

.url-suggestion-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.url-suggestion-url {
  font-size: 12px;
  color: #1a73e8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.url-suggestion-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.url-suggestion-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: #666;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.2s, color 0.2s;
}

.url-suggestion-btn:hover {
  background: #f1f2f6;
  color: #ff4757;
}

.btn-share {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 6px;
  background: #f1f2f6;
  color: #ff4757;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  transition: background 0.2s;
  flex-shrink: 0;
}

.btn-share:hover {
  background: #ff4757;
  color: #fff;
}

.url-bar .btn {
  padding: 6px 12px;
  font-size: 13px;
}

/* === Iframe Container === */
.iframe-wrap {
  flex: 1;
  position: relative;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  background: #fff;
}

.iframe-wrap iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

/* === Draggable Avatar === */
.avatar-layer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1000;
  touch-action: none;
}

.avatar-wrap {
  position: absolute;
  pointer-events: auto;
  cursor: grab;
  touch-action: none;
  transform-origin: center center;
  will-change: transform;
}

.avatar-wrap.dragging {
  cursor: grabbing;
}

.avatar-wrap img {
  display: block;
  width: 120px;
  height: auto;
  max-width: none;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: auto; /* Allow drag on image center area */
  cursor: grab;
}

/* === 4 Corner Handles (TL/TR/BR/BL) === */
.avatar-corner {
  position: absolute;
  width: 20px;
  height: 20px;
  background: #ff4757;
  border: 2px solid #fff;
  border-radius: 50%;
  z-index: 10;
  opacity: 0;
  pointer-events: auto;
  touch-action: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
  transition: opacity 0.2s ease;
}

.avatar-corner.corner-tl { top: -10px; left: -10px; cursor: nwse-resize; }
.avatar-corner.corner-tr { top: -10px; right: -10px; cursor: nesw-resize; }
.avatar-corner.corner-br { bottom: -10px; right: -10px; cursor: nwse-resize; }
.avatar-corner.corner-bl { bottom: -10px; left: -10px; cursor: nesw-resize; }

/* === Show corners checkbox label === */
.corner-toggle {
  display: inline-flex;
  align-items: center;
  font-size: 13px;
  color: #666;
  cursor: pointer;
  margin-left: 8px;
  white-space: nowrap;
  flex-shrink: 0;
}
.corner-toggle input { cursor: pointer; }

/* === Crop Preview === */
.crop-preview-wrap {
  margin: 10px 0;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px;
  background: #fafafa;
}
.crop-layout {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.crop-main {
  flex: 1;
  min-width: 0;
}
.crop-container {
  position: relative;
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
.crop-container img {
  max-width: 100%;
  max-height: 260px;
  display: block;
}
.crop-shade {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.5);
  pointer-events: none;
}
.crop-frame {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.3);
  cursor: move;
  touch-action: none;
}
.crop-frame-inner {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  border: 1px dashed rgba(255,255,255,0.6);
  pointer-events: none;
}
.crop-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #fff;
  border: 1px solid #ff4757;
  border-radius: 50%;
  z-index: 2;
  touch-action: none;
}
.crop-handle[data-handle="nw"] { top: -6px; left: -6px; cursor: nw-resize; }
.crop-handle[data-handle="ne"] { top: -6px; right: -6px; cursor: ne-resize; }
.crop-handle[data-handle="sw"] { bottom: -6px; left: -6px; cursor: sw-resize; }
.crop-handle[data-handle="se"] { bottom: -6px; right: -6px; cursor: se-resize; }
.crop-hint {
  font-size: 12px;
  color: #888;
  margin-top: 6px;
  text-align: center;
}
.crop-preview-panel {
  width: 80px;
  flex-shrink: 0;
  text-align: center;
}
.crop-preview-label {
  font-size: 11px;
  color: #666;
  margin-bottom: 6px;
}
.crop-preview-thumb {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  border: 1px solid #ddd;
  overflow: hidden;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

/* === Modal / Forms === */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  opacity: 0;
  visibility: hidden;
  transition: all 0.2s ease;
  -webkit-transform: translateZ(0);
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: #fff;
  border-radius: 12px;
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  transform: scale(0.9);
  transition: transform 0.2s ease;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  -webkit-transform: translateZ(0);
}

.modal-overlay.active .modal {
  transform: scale(1);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #fff;
  border-radius: 12px 12px 0 0;
  position: relative;
  z-index: 1;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  line-height: 1;
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 12px 20px 20px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 6px;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  background: #fff;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: #ff4757;
  box-shadow: 0 0 0 2px rgba(255,71,87,0.1);
}

.form-group input[type="file"] {
  padding: 8px;
}

.form-hint {
  font-size: 12px;
  color: #999;
  margin-top: 4px;
}

.error-msg {
  color: #ff4757;
  font-size: 13px;
  margin-top: 6px;
  display: none;
}

/* === Admin Panel === */
.admin-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  min-width: 640px;
}

.admin-table th,
.admin-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.admin-table th {
  background: #f8f9fa;
  font-weight: 600;
  color: #555;
  position: sticky;
  top: 0;
}

.admin-table tr:hover td {
  background: #f8f9fa;
}

.admin-table .avatar-thumb {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  vertical-align: middle;
}

.admin-table .actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

/* === Admin Page: allow scroll on body === */
body.admin-body {
  overflow-y: auto;
  overflow-x: hidden;
}

/* === Admin Page Layout === */
.admin-page {
  padding: 16px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: 100vh;
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 8px;
}

.admin-header h1 {
  font-size: 22px;
  color: #333;
}

.admin-card {
  background: #fff;
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.admin-card-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
}

/* === Toast === */
.toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 14px;
  color: #fff;
  pointer-events: auto;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  white-space: nowrap;
}

.toast.success { background: #2ed573; }
.toast.error   { background: #ff4757; }
.toast.info    { background: #1e90ff; }

@keyframes toastIn {
  from { opacity: 0; transform: translateY(-10px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateY(0); }
  to   { opacity: 0; transform: translateY(-10px); }
}

/* === Responsive === */
@media (max-width: 640px) {
  /* Admin table -> cards on mobile */
  .admin-table-wrap { overflow-x: visible; }
  .admin-table,
  .admin-table thead,
  .admin-table tbody,
  .admin-table tr,
  .admin-table th,
  .admin-table td { display: block; width: 100%; }
  .admin-table thead { display: none; }
  .admin-table tr {
    margin-bottom: 12px;
    border: 1px solid #eee;
    border-radius: 8px;
    padding: 12px;
    background: #fff;
  }
  .admin-table td {
    border: none;
    padding: 6px 0;
    display: flex;
    align-items: baseline;
    gap: 8px;
  }
  .admin-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #666;
    font-size: 13px;
    white-space: nowrap;
    flex-shrink: 0;
    min-width: 60px;
  }
  .admin-table td:first-child { padding-left: 0; min-height: auto; }
  .admin-table td:first-child::before { display: none; }
  .admin-table .actions {
    padding-left: 0;
    margin-top: 8px;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
  }
  .admin-table .actions::before { display: none; }

  .modal-overlay {
    align-items: center !important;
    justify-content: center !important;
    padding: 20px !important;
  }

  .modal {
    width: calc(100% - 40px) !important;
    max-width: 380px !important;
    border-radius: 12px !important;
    position: relative !important;
    margin: auto !important;
    align-self: center !important;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    top: auto !important;
    transform: scale(0.9) !important;
  }

  .modal-overlay.active .modal {
    transform: scale(1) !important;
  }

  .modal-header {
    background: #fff !important;
    border-radius: 12px 12px 0 0 !important;
    position: relative !important;
    z-index: 2 !important;
  }

  .top-bar {
    padding: 4px 6px;
    min-height: 36px;
  }

  .top-bar-left {
    gap: 4px;
    min-width: 0;
    overflow: hidden;
    flex: 1;
  }

  .site-logo {
    font-size: 13px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-block;
    white-space: nowrap;
    flex-shrink: 0;
    max-width: 120px;
  }

  .usage-tip {
    font-size: 11px;
    color: #888;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
    display: block;
  }

  /* Mobile: hide desktop-only elements inside user-actions */
  .user-actions .desktop-only {
    display: none !important;
  }

  /* Mobile: compact login/register buttons */
  .user-actions .btn-small {
    padding: 3px 8px;
    font-size: 11px;
    min-height: 26px;
  }

  .user-actions {
    gap: 3px;
  }

  /* Mobile: shrink avatar-mini in top bar */
  .user-actions .avatar-mini {
    width: 24px;
    height: 24px;
    border-width: 2px;
  }

  .corner-toggle {
    font-size: 11px;
    margin-left: 0;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .corner-toggle input {
    width: 14px;
    height: 14px;
    margin: 0 2px 0 0;
  }

  .avatar-menu-trigger {
    cursor: pointer;
    transition: transform 0.2s ease;
  }
  .avatar-menu-trigger:active {
    transform: scale(0.95);
  }

  .mobile-menu-user .avatar-mini {
    width: 36px;
    height: 36px;
  }

  .btn {
    padding: 4px 8px;
    font-size: 12px;
  }

  .avatar-mini {
    width: 24px;
    height: 24px;
  }

  .modal {
    width: calc(100% - 32px);
    max-width: 400px;
    border-radius: 12px;
    position: relative;
    bottom: auto;
    left: auto;
    right: auto;
    top: auto;
    max-height: 85vh;
    margin: 0 auto;
  }

  .modal-overlay.active .modal {
    transform: scale(1);
  }

  .url-bar {
    padding: 4px 8px;
  }

  .url-bar input {
    font-size: 12px;
  }

  .avatar-wrap img {
    width: 80px;
  }

  .resize-handle,
  .rotate-handle,
  .delete-handle {
    width: 20px;
    height: 20px;
  }

  .admin-page {
    padding: 10px;
  }

  .admin-table th,
  .admin-table td {
    padding: 8px 10px;
    font-size: 13px;
  }

  /* Hamburger button */
  .btn-hamburger {
    display: inline-block;
    padding: 2px 4px;
    font-size: 18px;
  }

  /* Corner toggle checkbox only */
  .corner-toggle {
    font-size: 11px;
    margin-left: 2px;
    display: flex;
    align-items: center;
  }
  .corner-label {
    display: none;
  }
  .corner-toggle input {
    width: 14px;
    height: 14px;
    margin: 0;
  }
}

/* Desktop: show user-actions, hide mobile elements */
@media (min-width: 641px) {
  .btn-hamburger { display: none !important; }
  .mobile-menu { display: none !important; }
  .user-actions { display: flex !important; }
}

/* === Landscape / Tablet === */
@media (min-width: 641px) and (max-width: 1024px) {
  .usage-tip {
    font-size: 12px;
  }

  .avatar-wrap img {
    width: 100px;
  }
}

/* === Loading State === */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #999;
  font-size: 14px;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid #e0e0e0;
  border-top-color: #ff4757;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 8px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === Hidden === */
.hidden {
  display: none !important;
}

/* === Mobile Menu (hamburger) === */
.btn-hamburger {
  display: none;
  background: none;
  border: none;
  font-size: 22px;
  color: #666;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

.mobile-menu {
  display: none;
  position: absolute;
  top: 48px;
  left: 0;
  right: 0;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  z-index: 99;
  padding: 8px 0;
}
.mobile-menu.active {
  display: block;
}
.mobile-menu-content {
  display: flex;
  flex-direction: column;
}
.mobile-menu-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid #eee;
  margin-bottom: 4px;
}
.mobile-menu-user span {
  font-size: 15px;
  font-weight: 600;
  color: #333;
}
.mobile-menu-item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  border: none;
  background: none;
  text-align: left;
  font-size: 15px;
  color: #333;
  cursor: pointer;
  text-decoration: none;
}
.mobile-menu-item:hover,
.mobile-menu-item:active {
  background: #f5f5f5;
}
.mobile-menu-primary {
  color: #ff4757;
  font-weight: 600;
}

.top-bar-right {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

/* Desktop: hide hamburger, show desktop buttons */
@media (min-width: 641px) {
  .btn-hamburger { display: none !important; }
  .mobile-menu { display: none !important; }
  .user-actions { display: flex !important; }
}
