/* CSS 변수 정의 */
.image-crop-tool-wrapper {
  --bg-primary: #f5f5f0;
  --bg-secondary: #fff;
  --bg-section: #fff;
  --border-color: #333;
  --text-primary: #222;
  --text-secondary: #555;
  --text-light: #666;
  --color-accent: #3498db;
  --color-success: #27ae60;
  --color-warning: #f39c12;
  --color-danger: #e74c3c;
  --color-purple: #9b59b6;
  --shadow-sm: 2px 2px 0 rgba(0,0,0,0.1);
  --shadow-md: 4px 4px 0 rgba(0,0,0,0.15);
  --shadow-lg: 8px 8px 0 rgba(0,0,0,0.15);
}

/* 전역 box-sizing */
.image-crop-tool-wrapper,
.image-crop-tool-wrapper *,
.image-crop-tool-wrapper *::before,
.image-crop-tool-wrapper *::after {
  box-sizing: border-box;
}

/* 메인 컨테이너 */
.image-crop-tool-container {
  max-width: 1180px;
  width: 100%;
  margin: 15px auto;
  padding: 15px;
  background: var(--bg-primary);
  border: 2px solid var(--border-color);
  font-family: 'Noto Sans KR', 'Malgun Gothic', -apple-system, BlinkMacSystemFont, sans-serif;
  color: var(--text-primary);
  box-shadow: var(--shadow-lg);
  line-height: 1.5;
}

/* 헤더 */
.tool-header {
  text-align: center;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 3px double var(--border-color);
}

.tool-header h2 {
  margin: 0 0 6px 0;
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  text-shadow: 2px 2px 0 rgba(255,255,255,0.5);
  line-height: 1.2;
}

.subtitle {
  margin: 0;
  font-size: 15px;
  color: var(--text-light);
  line-height: 1.4;
}

/* 섹션 공통 */
.section {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 12px;
  box-shadow: var(--shadow-md);
  margin-bottom: 12px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 10px 0;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
  font-size: 17px;
  font-weight: 700;
  color: #2c3e50;
  line-height: 1.2;
}

/* 업로드 섹션 (독립) */
.upload-section {
  width: 100%;
}

/* 그리드 행 레이아웃 */
.grid-row-1,
.grid-row-2,
.grid-row-3 {
  display: grid;
  gap: 12px;
  margin-bottom: 12px;
}

/* 첫 번째 행: 업로드 50% + 컨트롤 50% */
.grid-row-1 {
  grid-template-columns: 1fr 1fr;
}

/* 두 번째 행: 크롭 영역 50% + 미리보기 50% */
.grid-row-2 {
  grid-template-columns: 1fr 1fr;
}

/* 세 번째 행: 다운로드 100% */
.grid-row-3 {
  grid-template-columns: 1fr;
}

/* 업로드 섹션 */
.upload-area {
  border: 2px dashed var(--border-color);
  background: #fafafa;
  padding: 30px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
}

.upload-area:hover {
  background: #f0f0f0;
  border-color: var(--color-accent);
}

.upload-area.dragging {
  background: #e3f2fd;
  border-color: var(--color-accent);
}

.upload-content svg {
  color: var(--text-light);
  margin-bottom: 12px;
}

.upload-text {
  margin: 0 0 6px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.upload-hint {
  margin: 0;
  font-size: 14px;
  color: var(--text-light);
}

.image-info {
  margin-top: 12px;
  padding: 10px;
  background: #f9f9f9;
  border: 1px solid #ddd;
}

.info-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
  font-size: 14px;
}

.info-item:last-child {
  margin-bottom: 0;
}

.info-label {
  font-weight: 600;
  color: var(--text-secondary);
}

.info-value {
  color: var(--text-primary);
  font-family: 'Courier New', monospace;
}

/* 컨트롤 섹션 */
.control-group {
  margin-bottom: 12px;
}

.control-group:last-child {
  margin-bottom: 0;
}

.control-label {
  margin: 0 0 8px 0;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
}

.button-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn-ratio {
  flex: 1;
  min-width: 60px;
  padding: 8px 10px;
  background: #fff;
  border: 1px solid var(--border-color);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 2px 2px 0 rgba(0,0,0,0.15);
  font-family: inherit;
}

.btn-ratio:hover:not(:disabled) {
  background: #f0f0f0;
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0 rgba(0,0,0,0.15);
}

.btn-ratio.active {
  background: var(--color-accent);
  color: #fff;
}

.btn-ratio:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-control {
  flex: 1;
  min-width: 120px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--border-color);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 2px 2px 0 rgba(0,0,0,0.15);
  font-family: inherit;
}

.btn-control:hover:not(:disabled) {
  background: #f0f0f0;
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0 rgba(0,0,0,0.15);
}

.btn-control:active:not(:disabled) {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.btn-control:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-control:focus-visible {
  outline: 2px solid var(--color-warning);
  outline-offset: 2px;
}

.btn-control.full-width {
  width: 100%;
}

.btn-reset {
  background: var(--color-warning);
  color: #fff;
}

/* 다운로드 섹션 */
.download-section {
  width: 100%;
}

.download-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.format-selector {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.format-selector label {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-secondary);
  white-space: nowrap;
}

.btn-download-large {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 24px;
  background: var(--color-success);
  color: #fff;
  border: 2px solid var(--border-color);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 4px 4px 0 rgba(0,0,0,0.2);
  font-family: inherit;
}

.btn-download-large:hover:not(:disabled) {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}

.btn-download-large:active:not(:disabled) {
  transform: translate(4px, 4px);
  box-shadow: none;
}

.btn-download-large:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-download-large:focus-visible {
  outline: 3px solid var(--color-warning);
  outline-offset: 2px;
}

.download-options {
  display: flex;
  align-items: center;
  gap: 10px;
}

.download-options label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}

.format-select {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  background: #fff;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  font-family: inherit;
  cursor: pointer;
}

.format-select:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.format-select:focus {
  outline: none;
  border-color: var(--color-warning);
  background: #fffacd;
}

.btn-download {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--color-success);
  color: #fff;
  border: 1px solid var(--border-color);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 3px 3px 0 rgba(0,0,0,0.2);
  font-family: inherit;
}

.btn-download:hover:not(:disabled) {
  transform: translate(1px, 1px);
  box-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}

.btn-download:active:not(:disabled) {
  transform: translate(3px, 3px);
  box-shadow: none;
}

.btn-download:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-download:focus-visible {
  outline: 3px solid var(--color-warning);
  outline-offset: 2px;
}

.btn-reset {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--color-warning);
  color: #fff;
  border: 1px solid var(--border-color);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 3px 3px 0 rgba(0,0,0,0.2);
  font-family: inherit;
  margin-top: 10px;
}

.btn-reset:hover:not(:disabled) {
  transform: translate(1px, 1px);
  box-shadow: 2px 2px 0 rgba(0,0,0,0.2);
}

.btn-reset:active:not(:disabled) {
  transform: translate(3px, 3px);
  box-shadow: none;
}

.btn-reset:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-reset:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

/* 크롭 영역 섹션 */
.crop-container {
  min-height: 500px;
  background: #f9f9f9;
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

/* 미리보기 섹션 */
.preview-container {
  min-height: 500px;
  background: #f9f9f9;
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  position: relative;
  padding: 12px;
}

#previewCanvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
  border: 2px solid var(--color-success);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.crop-placeholder {
  text-align: center;
  color: var(--text-light);
}

.crop-placeholder svg {
  margin-bottom: 12px;
  opacity: 0.5;
}

.crop-placeholder p {
  margin: 0;
  font-size: 15px;
  line-height: 1.6;
}

.crop-workspace {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

#canvas {
  max-width: 100%;
  max-height: 500px;
  display: block;
}

.crop-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.crop-box {
  position: absolute;
  border: 2px solid var(--color-accent);
  background: rgba(52, 152, 219, 0.1);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
  pointer-events: all;
  cursor: move;
}

.crop-handle {
  position: absolute;
  background: var(--color-accent);
  border: 2px solid #fff;
  box-shadow: 0 0 3px rgba(0,0,0,0.5);
}

.crop-handle.corner {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.crop-handle.edge {
  background: transparent;
  border: none;
  box-shadow: none;
}

.crop-handle.corner.top-left {
  top: -6px;
  left: -6px;
  cursor: nw-resize;
}

.crop-handle.corner.top-right {
  top: -6px;
  right: -6px;
  cursor: ne-resize;
}

.crop-handle.corner.bottom-left {
  bottom: -6px;
  left: -6px;
  cursor: sw-resize;
}

.crop-handle.corner.bottom-right {
  bottom: -6px;
  right: -6px;
  cursor: se-resize;
}

.crop-handle.edge.top {
  top: -3px;
  left: 10%;
  right: 10%;
  height: 6px;
  cursor: n-resize;
}

.crop-handle.edge.bottom {
  bottom: -3px;
  left: 10%;
  right: 10%;
  height: 6px;
  cursor: s-resize;
}

.crop-handle.edge.left {
  left: -3px;
  top: 10%;
  bottom: 10%;
  width: 6px;
  cursor: w-resize;
}

.crop-handle.edge.right {
  right: -3px;
  top: 10%;
  bottom: 10%;
  width: 6px;
  cursor: e-resize;
}

.crop-info {
  position: absolute;
  top: -30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--color-accent);
  color: #fff;
  padding: 4px 12px;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* 정보 섹션 */
.info-content {
  font-size: 15px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 12px;
}

.info-card {
  padding: 12px;
  background: #f9f9f9;
  border: 1px solid #ddd;
  text-align: center;
}

.info-icon {
  font-size: 24px;
  margin-bottom: 6px;
}

.info-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.info-desc {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

.feature-list {
  margin-bottom: 12px;
}

.feature-list p {
  margin: 0 0 8px 0;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

.feature-list ul {
  margin: 0;
  padding-left: 20px;
  list-style-type: disc;
}

.feature-list li {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  line-height: 1.5;
}

.feature-list li:last-child {
  margin-bottom: 0;
}

.tip-box {
  padding: 12px;
  background: #e8f5e9;
  border: 1px solid var(--color-success);
  border-left: 4px solid var(--color-success);
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-bottom: 12px;
}

.tip-box strong {
  color: var(--color-success);
  display: block;
  margin-bottom: 6px;
}

.tip-box ul {
  margin: 0;
  padding-left: 20px;
  list-style-type: disc;
}

.tip-box li {
  margin-bottom: 4px;
}

.tip-box li:last-child {
  margin-bottom: 0;
}

.warning-box {
  padding: 12px;
  background: #fff8e6;
  border: 1px solid var(--color-warning);
  border-left: 4px solid var(--color-warning);
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.warning-box strong {
  color: var(--color-warning);
}

/* 반응형: 태블릿 */
@media (max-width: 1024px) {
  .grid-row-1,
  .grid-row-2 {
    grid-template-columns: 1fr;
  }
  
  .crop-container,
  .preview-container {
    min-height: 400px;
  }
  
  .info-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .download-controls {
    flex-direction: column;
    align-items: stretch;
  }
  
  .format-selector {
    width: 100%;
  }
}

/* 반응형: 모바일 */
@media (max-width: 768px) {
  .image-crop-tool-container {
    margin: 8px 5px;
    padding: 10px;
    width: calc(100% - 10px);
    max-width: 100%;
    box-sizing: border-box;
  }
  
  .tool-header h2 {
    font-size: 24px;
  }
  
  .subtitle {
    font-size: 14px;
  }
  
  .grid-row-1,
  .grid-row-2 {
    grid-template-columns: 1fr;
  }
  
  .upload-area {
    padding: 20px;
  }
  
  .button-row {
    flex-direction: column;
  }
  
  .btn-ratio,
  .btn-control {
    width: 100%;
  }
  
  .download-controls {
    flex-direction: column;
    align-items: stretch;
  }
  
  .format-selector {
    width: 100%;
    margin-bottom: 8px;
  }
  
  .btn-download-large {
    width: 100%;
    justify-content: center;
  }
  
  .crop-container,
  .preview-container {
    min-height: 350px;
  }
  
  #canvas,
  #previewCanvas {
    max-height: 350px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
}

/* 반응형: 작은 모바일 */
@media (max-width: 480px) {
  .image-crop-tool-container {
    margin: 8px 5px;
    padding: 8px;
    width: calc(100% - 10px);
    max-width: 100%;
    box-sizing: border-box;
    border-width: 1px;
  }
  
  .tool-header h2 {
    font-size: 22px;
  }
  
  .upload-area {
    padding: 15px;
  }
  
  .upload-content svg {
    width: 40px;
    height: 40px;
  }
  
  .upload-text {
    font-size: 15px;
  }
  
  .crop-container {
    min-height: 300px;
  }
  
  #canvas {
    max-height: 300px;
  }
  
  .crop-handle.corner {
    width: 16px;
    height: 16px;
  }
  
  .crop-handle.corner.top-left {
    top: -8px;
    left: -8px;
  }
  
  .crop-handle.corner.top-right {
    top: -8px;
    right: -8px;
  }
  
  .crop-handle.corner.bottom-left {
    bottom: -8px;
    left: -8px;
  }
  
  .crop-handle.corner.bottom-right {
    bottom: -8px;
    right: -8px;
  }
}